以下是使用Java语言解决该问题的示例代码:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList<>();
arrayList.add(10);
arrayList.add(20);
arrayList.add(30);
arrayList.add(40);
arrayList.add(50);
int maxIndex = findMaxIndex(arrayList);
System.out.println("Max index: " + maxIndex);
}
public static int findMaxIndex(ArrayList arrayList) {
int maxIndex = 0;
int maxValue = Integer.MIN_VALUE;
for (int i = 0; i < arrayList.size(); i++) {
if (arrayList.get(i) > maxValue) {
maxValue = arrayList.get(i);
maxIndex = i;
}
}
return maxIndex;
}
}
在上述示例代码中,我们首先创建了一个ArrayList,并向其添加了一些整数值。然后,我们调用findMaxIndex
方法来查找ArrayList中的最大项的索引。该方法使用一个循环来遍历ArrayList,并通过比较每个项的值来更新maxIndex
和maxValue
变量。最后,我们在控制台打印出最大项的索引。
运行该代码将输出:
Max index: 4
表示最大项的索引为4。