打印数组的索引

时间:2013-11-20 20:49:21

标签: arrays netbeans indexing

正如标题所说,我有一个数组,我只需要打印索引。我不知道我在做什么。我尝试过几件事。这是最新的。谢谢你的帮助!

public static void evenIndex(int[] array){
    int length = array.length;

    for (int i = 0; i < length; i++) {
        while (array[i] == 0,2,4,6,8){
            System.out.print(array[i]);
        }

    }
}

3 个答案:

答案 0 :(得分:0)

public static void evenIndex(int[] array){
    int length = array.length;

    for (int i = 0; i < length; i++) {
        if (i%2 == 0){ // make sure i is even
            System.out.print(array[i]); // the value of the array @ index i
            System.out.print(i); // the index ?
        }
    }
}

这是你在找什么?

答案 1 :(得分:0)

如果你需要打印索引,那么在最后一行打印i而不是array [i]

答案 2 :(得分:0)

如果您只想打印索引,那么您需要做的就是在匹配时打印变量'i',因为这是您当前的位置或数组中的索引:

public static void evenIndex(int[] array){
    int length = array.length;

    for (int i = 0; i < length; i++) {
        while (array[i] == 0,2,4,6,8){
            //the following will print eg. index 0 = 0, index 2 = 2, 
            //is this what is required?
            System.out.print('index ' + i + '=' + array[i]);
        }

    }
}