如何用 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}

更新於: 19-Feb-2020

338 瀏覽次數

開啟你的 職業生涯

完成課程認證

開始吧
廣告
© . All rights reserved.