java定义3个一维数组,长度自行定义即可并赋值 定义一个行数为3的二维数组,把上述的3个一维数组复制到这个二维数组中,并遍历打印
时间: 2024-02-18 07:06:08 浏览: 68
好的,我来为你解答这个问题。
首先,我们可以使用以下代码定义并赋值三个一维数组:
```java
int[] arr1 = {1, 2, 3};
int[] arr2 = {4, 5, 6};
int[] arr3 = {7, 8, 9};
```
接下来,我们可以使用以下代码定义一个行数为3的二维数组,并将上述三个一维数组复制到二维数组中:
```java
int[][] arr2D = new int[3][];
arr2D[0] = arr1;
arr2D[1] = arr2;
arr2D[2] = arr3;
```
最后,我们可以使用以下代码遍历并打印二维数组中的元素:
```java
for (int i = 0; i < arr2D.length; i++) {
for (int j = 0; j < arr2D[i].length; j++) {
System.out.print(arr2D[i][j] + " ");
}
System.out.println();
}
```
完整代码如下:
```java
public class Main {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3};
int[] arr2 = {4, 5, 6};
int[] arr3 = {7, 8, 9};
int[][] arr2D = new int[3][];
arr2D[0] = arr1;
arr2D[1] = arr2;
arr2D[2] = arr3;
for (int i = 0; i < arr2D.length; i++) {
for (int j = 0; j < arr2D[i].length; j++) {
System.out.print(arr2D[i][j] + " ");
}
System.out.println();
}
}
}
```
输出结果为:
```
1 2 3
4 5 6
7 8 9
```
希望能帮到你!
阅读全文
相关推荐

















