在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开始的,所以在赋值和访问数组元素时要注意索引范围。

