Multidimensional Arrays
Two-dimensional array
0. Array Features
Is a reference to the array type itself
must specify the length of time a statement once it is determined to change can not happen again
1. Definition / Statement
Type stored inside [the name of the array;
int [] [] arrray;
2. Initialize
Static - all elements having a length
int [] [] array = [ {1,2,3}, {4,5,6,7}, {7,8,9}};
dynamic element length is not only ---- (default);
int [] [] Array = new new [. 3] [2];
Array -> {{X, X}, {X, X}, {X, X}} -> X default 0
may produce a runtime exceptions
NullPointerException
3. element access
By -index element position in the array
Array [I] [j]
I position control of large arrays of small arrays
j controlling the position of a small array elements
4. Loop poll
Enhanced for loop nested loop normal completion
The structure types are stored in the reference memory
Two-dimensional array structure stored in memory
int [] [] Array = new new int [3] [2];
// 3 each represent a 3-small array int []
// 2 represent each array has two small elements
// Array [0] [0] = 10;
// Array [0] [. 1] 2 = 0;
public class TestArray {
public static void main(String [] args){
int[][] array=new int[3][2];
array[0][0]=10;
array[0][3]=20;
array[1]=array[0];
array[0]=new int[4];
array[0][0]=100;
System.out.println(array[1][0]); //10
//二维数组的动态初始化
// int[][] array=new int[3][];//{{10,20},{0,0},{0,0}}
//array[0]=new int[2];
//array[0][0]=10;
//array[0][4]=20;
//array[0][2]=30; //运行时异常 ArrayIndexOutOfBoundsException
/*
//1.声明----初始化(静态)
int[][] array=new int[][]{{1,2},{3,4,5,6},{7,8,9}};
//访问数组内的元素 ---index
//int value= array[0][0]; //前面表示小数组位置 后面表示小数组中元素的位置
//System.out.println(value); // 1 array[2][3] 6 array[2][2] 9
//数组的遍历/轮询
for(int i=0;i<array.length;i++){ //遍历大数组中的小数组
for(int j=0;j<array[i].length;j++){ //遍历小数组中的元素
int value=array[i][j];
System.out.print(value+"\t");
}
System.out.println();
}
System.out.println("----------------");
for(int[]arr: array){
for(int value:arr){
System.out.println(value);
}
}*/
}
}
Three-dimensional array
Create a good array stores a plurality of array int [] []
int [] [] [] = new new Array {{{1,2}, {3,4-}}, {{}, {}}, {{}, }}} {
int [] [] [] array = new new int [. 3] [] [];
each of which a three-dimensional array has a two dimensional array then there are two elements int
Two-dimensional array of exercises
1. Analog classmates weekly change position
{{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}}
import java.util.Scanner;
public class ChangeGroup {
public static void main(String[] args){
//创建一个二维数组 用来表示班级里的每一列同学
int[][] array={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
//设计每一周交换一次 几周?
Scanner input =new Scanner(System.in);
System.out.println("请输入一个周数,我来告诉您交换后的结果");
int week=input.nextInt(); //6
for(int i=1;i<week%4;i++){
int[] x= array[0];
array[0]=array[1];
array[1]=array[2];
array[2]=array[3];
array[3]= x;
}
for(int[] arr:array) {
for(int v: arr){
System.out.print(v+"\t");
}
System.out.println();
}
}
}