声明和初始化
int[][] a1 = new int[2][3]; //声明并创建
int[][] a2 = {{1,2,3},{4,5,6}}; //声明并初始化
int[][] a3 = new int[][]{{1,2,3},{4,5,6}}; //声明、创建并初始化
int[][] a4; //先声明一个二维整形数组
a4 = new int[2][3]; //使用new运算符创建并赋值给数组
int[][]a5; //声明整形数组
a5 = new int[][] {{1,2,3},{4,5,6}}; //创建数组并初始化数组、赋值给数组
int[][]a6 = new int[2][2]; //声明并初始化一个二维整形数组
a6[0][0] = 1;a6[0][1] = 2; //利用赋值语句初始化该实例
a6[1][0] = 3;a6[1][1] = 4;
二维数组的基本访问操作
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]+" ");
}
System.out.println(Arrays.deepToString(array));
二维数组的拷贝
Arrays.copyOf
// An highlighted block
var foo = 'bar';
class TestArray {
private int val;
public void setVal(int val) {
this.val = val;
}
public int getVal() {
return this.val;
}
}
public class Demo5 {
public static void main(String[] args) {
TestArray[][] array = new TestArray[2][2];
array[0][0] = new TestArray();
array[0][1] = new TestArray();
array[1][0] = new TestArray();
array[1][1] = new TestArray();
TestArray[][] array1 = new TestArray[2][2];
System.out.println(array1);
System.out.println(array);
array1 = Arrays.copyOf(array,array.length);
System.out.println(array1);
System.out.println(array);
System.out.println("=============拷贝完成=========");
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j].getVal()+" ");
}
}
System.out.println();
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array1[i].length; j++) {
System.out.print(array1[i][j].getVal()+" ");
}
}
System.out.println();
array[0][0].setVal(100);
System.out.println("============修改完成=========");
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j].getVal()+" ");
}
}
System.out.println();
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array1[i].length; j++) {
System.out.print(array1[i][j].getVal()+" ");
}
}
System.out.println();
}
}
System.arraycopy
System.arraycopy(testArray1[i],0,testArray2[i],0,
testArray1[i].length);
clone
for (int i = 0; i <testArray.length ; i++) {
testArray1[i] = testArray[i].clone();
}
魔方矩阵
/*将数字1放到,第一行的中间位置
新来的数字,放到当前数字的上一行,下一列
如果上一行下一列,有数字,把要放入的数字,放到当前数字的下一行*/
public class Demo5 {
public static int[][] magicArray(int n) {
int[][] array = new int[n][n];
int num = 1; //放入的数字
int x = 0; //列
int y = n/2; //第一行中间位置
for (int i = 0; i<n*n; i++) {
array[x][y] = num;
if (num % n == 0) { //如果放入的数字为n的整数倍则放到当前数下一行
y++;
} else {
x--; //如果不是,则放在当前数的上一行,下一列
y++;
}
if (x < 0) { //行超出范围
x = n-1; //放到最右边
}
if (y > n-1) { //列超出范围
y = 0; //放在第一列
}
num++; //数字加一
}
return array;
}
public static void main(String[] args) {
int n = 2;
int[][] array = new int[n][n];
magicArray(n);
for (int i = 0; i < magicArray(n).length; i++) {
System.out.print(Arrays.toString(magicArray(n)[i]));
System.out.println();
}
}
}
/*
[8, 1, 6]
[3, 5, 7]
[4, 9, 2]
*/
public static void magicScqure(int[][] array,int n) {//3 1- 9
array[0][n/2] = 1;
int prevRow = 0;
int prevCol = n/2;
for(int i = 2;i <= n*n;i++) {
if(array[(prevRow-1+n)%n][(prevCol+1)%n] != 0) {
//上一行的下一列没有数据
prevRow = (prevRow+1)%n;//下一行
} else {
prevRow = (prevRow-1+n)%n;
prevCol = (prevCol+1)%n;
}
array[prevRow][prevCol] = i;
}
}
public static void main(String[] args) {
int row = 5;
int[][] array = new int[row][row];
magicScqure(array,row);
System.out.println(Arrays.deepToString(array));
}