在Java中,可以使用以下方式定義和初始化數組:
int[] numbers = new int[5];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
int[] numbers = {10, 20, 30, 40, 50};
int[] numbers = new int[5];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = (i + 1) * 10;
}
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
需要注意的是,數組的索引是從0開始的,所以在賦值和訪問數組元素時要注意索引范圍。