在Java中,数组的最大下标可以通过获取数组的长度然后减去1来得到。下面是一个简单的示例代码,展示了如何计算一个整数数组的最大下标:
public class MaxIndex {public static void main(String[] args) {int[] array = {1, 2, 3, 4, 5}; // 示例数组int maxIndex = array.length - 1; // 计算最大下标System.out.println("数组的最大下标是: " + maxIndex);}}
如果你需要找到数组中最大值的下标,你可以使用一个循环来遍历数组,记录当前最大值及其下标,循环结束后输出最大值的下标。下面是一个示例代码:

public class MaxIndex {public static void main(String[] args) {int[] array = {1, 2, 3, 4, 5}; // 示例数组int max = array; // 假设第一个元素是最大值int maxIndex = 0; // 最大值的下标初始化为0for (int i = 1; i < array.length; i++) { // 从第二个元素开始遍历if (array[i] > max) { // 如果当前元素大于最大值max = array[i]; // 更新最大值maxIndex = i; // 更新最大值的下标}}System.out.println("数组中最大值的下标是: " + maxIndex);}}
以上代码会输出数组中最大值的下标。
