如何在 Java 中宣告、建立、初始化和訪問陣列?


你可以像變數一樣宣告一個數組 −

int myArray[];

你可以像使用 new 關鍵字建立物件一樣建立陣列 −

myArray = new int[5];

你可以透過使用索引將值一個個分配給所有元素來初始化陣列 −

myArray [0] = 101;
myArray [1] = 102;

你可以使用索引值訪問陣列元素 −

System.out.println("The first element of the array is: " + myArray [0]);
System.out.println("The first element of the array is: " + myArray [1]);
Alternatively, you can create and initialize an array using the flower braces ({ }):
Int [] myArray = {10, 20, 30, 40, 50}

更新於:2020-02-25

275 次瀏覽

開始您的 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.