如何在 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}
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP