-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxIndex.java
More file actions
27 lines (23 loc) · 827 Bytes
/
maxIndex.java
File metadata and controls
27 lines (23 loc) · 827 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//Exercise 7.3
public class maxIndex {
public static int indexOfMax(int[] a) {
//takes an array of integers and returns the index of the largest element.
int max = a[0];
int index;
for (int i = 0; i < a.length; i++) {
if (max < a[i]) {
max = a[i];
index = i;
}
}
return index;
}
public static int indexOfMaxEnhanced(int[] a) {
//same function as indexOfMax, but using an enhanced for loop.
//can't make it using enhanced for loop, because we are not just focusing on the value, but we are aiming to find the index.
}
public static void main(String[] args) {
int[] numbers = {1, 2, 2, 5, 4};
System.out.println(indexOfMax(numbers));
}
}