如何在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

624 次瀏覽

開啟你的職業

完成課程並獲得認證

開始
廣告
© . All rights reserved.