Use a variable named max
to store the largest element
Use a variable named indexOfMax
to denote the index of the largest element.
Initially max
is array[0]
, and indexOfMax
is 0
.
Compare each element in array
with max
, and update max
and indexOfMax
if the element is greater than max
.
public class Main { public static void main(String[] args) { int[] array = new int[5]; for (int i = 0; i < array.length; i++) { array[i] = (int)(Math.random() * 100); } /*from w w w .j av a2 s . com*/ for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } double max = array[0]; int indexOfMax = 0; for (int i = 1; i < array.length; i++) { if (array[i] > max) { max = array[i]; indexOfMax = i; } } System.out.println(max+" @ "+ indexOfMax); } }