• Java 資料結構 教程

Java 資料結構 - 遍歷陣列



為了處理陣列元素,我們通常使用for迴圈或foreach迴圈,因為陣列中的所有元素都是相同型別的,並且陣列的大小是已知的。假設我們有一個包含5個元素的陣列,我們可以列印該陣列的所有元素,如下所示:

示例

public class ProcessingArrays {		
   public static void main(String args[]) {
      int myArray[] = {22, 23, 25, 27, 30};       
      
      for(int i = 0; i<myArray.length; i++) {    	  
         System.out.println(myArray[i]);    	        
      }
   }   
}

輸出

22
23
25
27
30
廣告
© . All rights reserved.