在Java中,可以使用以下語法來定義和賦值數組:
int[] numbers = {1, 2, 3, 4, 5};
int[] numbers = new int[5];
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
numbers[4] = 5;
int[] numbers = new int[5];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = i + 1;
}
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[][] matrix = new int[3][3];
int value = 1;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = value;
value++;
}
}
以上是一些常見的定義和賦值數組的方法,根據具體的需求和情況,可以選擇合適的方式來定義和初始化數組。