2013-11-20 49 views
0

正如標題所說,我有一個數組,我只需要打印索引。我不知道我在做什麼。我嘗試了幾件事情。這是最新的。謝謝你的幫助!打印一個數組的索引

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]); 
     } 

    } 
} 

回答

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 ? 
     } 
    } 
} 

這是你在找什麼?

0

如果需要打印索引,然後在最後一行打印我的,而不是數組[我]

0

如果您正在尋找簡單地打印索引,所有你需要做的就是打印出來的變量「我」,當你有一個匹配,因爲這是您當前的位置或索引中的數組:

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]); 
     } 

    } 
}