如何在 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 年 2 月 25 日

275 次瀏覽

開啟你的 職業

完成該課程即可獲得認證

開始
廣告
© . All rights reserved.