1
1
123qwe2015-12-15 15:37:48
Java
123qwe, 2015-12-15 15:37:48

How to find the largest element in an array and display everything after it?

I'm already sweaty sitting with this.
I need the index of the maximum element, but how to get it - I'll never know. Google - I did not find it.

package com.winderton.study.mark;
 
public class Pascal2 {
    public static void main(String args[]) {
        
    int array[] = {1,2,6,-4,4,5,-2,-5,2,3};
    int max = 0;
    
    for (int i = 0; i < array.length; i++) {    
            if (array[i] > max) {
                    max = array[i];
                    
            }
            for (int j = i; j < array.length; j++){
                System.out.println(array[j]);
            }
        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
cthulhudx, 2015-12-15
@Yonghwa

int max = array[0];
int maxIndex = 0;    
    for (int index = 0; index < array.length; index++) {    
            if (array[index] > max) {
                    max = array[index];
                    maxIndex = index;
            }
    }

    for (int index = maxIndex; index < array.length; index++){
                System.out.println(array[index]);
    }

X
xD3ath, 2020-02-26
@xD3ath

public class ArrayMaxElement {
    public static void main(String[] args) {
        int arr[] = {1, 206, 10, 2, 3, 0, 7, 56, 32, 3, 14};
        int max = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        System.out.println(max);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question