# Two dimensional array

## definition:

`Is a container that holds the same data type (one-dimensional array)`

## Writing:

`Data type [] [] array name = initial value;`

## give an example:

```//  There are three one-dimensional arrays in the two-dimensional array. The length of each one-dimensional array is 4
int[][] array = new int[3][4];
//  The length of the printed 2D array is equivalent to how many elements there are in the printed array
System.out.println(array.length);
//  Definition method 2: you can define the elements in the array
/*
* Define a 2D array
* There are three one-dimensional arrays in two-dimensional array
* 1,2,3,4 / 2,34,234,11 / 23,44,66,55
*/

//  Three rows and four columns
int[][] array = new int[][]{
{1, 2, 3, 4},
{2, 34, 234, 11},
{23, 44, 66, 55}
};
//  Traversal array
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();
}```

# Multidimensional array:

## definition:

`Is a container that holds multiple data types of the same type (two-dimensional arrays)`

## give an example:

```//  There are two 2D arrays, three 1D arrays in each 2D array, four elements in each 1D array
//int[][][] arr = new int[2][3][4];
//  Define a three-dimensional array [2] [2] [3] {}
//  Three dimensional array of 2 layers, 2 rows and 3 columns --- mutton kebab
int[][][] arr = new int[][][] {
{
{1, 2, 3},
{1, 2, 4},
},
{
{2, 3, 4},
{1, 4, 5},
}
};
//  Traversal array
int sum = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
for (int k = 0; k < arr[i][j].length; k++) {
//System.out.print(arr[i][j][k] + " ");
sum = sum + arr[i][j][k];
}
//System.out.println();
}
//System.out.println();
}
System.out.println(sum);```

# Maze (2D array)

## analysis:

```1. Print map
Initial coordinate array[1][1]
End coordinate array[1][9]
2. How to get '0' up
Let '0' go by modifying the abscissa and ordinate
How to touch the wall without moving
You can take out the elements of the position you want to go first to judge whether the wall is a wall, it will not move or not, and then move
3. Loop input (the stop condition of the loop changes the coordinate position from 'to' 0 ')
If there is a clear stop condition, it is convenient to use while loop```

## code:

```//  How to print a map
public static void printArray(char[][] array) {
//  Print map
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();
}
}```
```//  There are 8 one-dimensional arrays, 10 elements in each one-dimensional array
//  array[1][9] - exit
//  array[1][1] d ---> array[1][1] == ' '
//                ---> array[1][2] == '0'
//  array[1][2] s ---> array[2][2] == '0'
char[][] array = {
//y   0   1   2   3   4   5   6   7   8   9
{'#','#','#','#','#','#','#','#','#','#'},//0 - x
{'#','0',' ',' ',' ',' ','#','#',' ',' '},//1
{'#','#',' ','#','#','#','#','#',' ','#'},//2
{'#',' ',' ',' ',' ','#','#','#',' ','#'},//3
{'#',' ','#','#',' ','#',' ',' ',' ','#'},//4
{'#',' ','#','#',' ','#',' ','#','#','#'},//5
{'#',' ',' ',' ',' ',' ',' ','#','#','#'},//6
{'#','#','#','#','#','#','#','#','#','#'},//7
};
//  Print map
printArray(array);
//  Declare the variable to use (start coordinate and end coordinate)
int x = 1;
int y = 1;
//  End
int ex = 1;
int ey = 9;
Scanner scanner = new Scanner(System.in);
//  Cycle input
while (array[ex][ey] != '0') {
//  Prompt for w a s d
System.out.println("Please enter w a s d");
String string = scanner.nextLine();
//  Determine where to move
switch (string) {
case "w":
if (array[x - 1][y] != '#') {
array[x][y] = ' ';
x = x - 1;
array[x][y] = '0';
}
break;
case "a":
if (array[x][y - 1] != '#') {
array[x][y] = ' ';
y = y - 1;
array[x][y] = '0';
}
break;
case "s":
if (array[x + 1][y] != '#') {
array[x][y] = ' ';
x = x + 1;
array[x][y] = '0';
}
break;
case "d":
//  Judge if it's a wall
if (array[x][y + 1] != '#') {
//  Change the original space
array[x][y] = ' ';
//  Move left modify coordinate + 1
y = y + 1;
//  New location changed to '0'
array[x][y] = '0';
}
break;
default:
break;
}
//  Print the map again after modification
printArray(array);
}