Home >Java >javaTutorial >Detailed explanation of arrays in Java summary
This article brings you relevant knowledge about java, which mainly introduces issues related to arrays, including naming rules, automatic type inference, static dynamic initialization, and other issues of multi-dimensional arrays Let’s take a look at writing methods, common mistakes in writing, etc. I hope it will be helpful to everyone.
Recommended study: "java Video Tutorial"
Index: Naming rules, automatic type inference, static dynamic initialization, multi-dimensional arrays Other ways of writing, common errors, array subscript out of bounds, default value issues, array length issues, array memory parsing
1. Step classification: (The following int can be replaced with char String double, etc.)
①One-step writing method:
int [] ids = new int []{1001,1002,1003};// 声明+初始化 int ids [] = new int []{1001,1002,1003}; // [ ]位置有两种放法 int ids [] = {1001,1002,1003};//自动类型推断,new int []可省 /**********错误示范***错误示范***错误示范***错误示范************/ int ids [] = new [] int{1001,1002,1003};//只有定义时的[]可以乱动
Automatic type inference : Only applicable in one-step writing method, you can omit = new int on the right [ ]
② Two-step writing method:
int ids [] ; //声明 ids = new int [ ] {1001,1002,1003} ; //初始化 /**********错误示范***错误示范***错误示范***错误示范************/ int ids []; ids [] = new int {1001,1002,1003};//分两步时,左边不加[] int ids []; ids = {1001,1002,1003};//分两步时,没有类型推断
2. Status classification: Static and dynamic must choose one of the two, do not add {} to the length, do not add the length to {}
## ① Static initialization:
两个[ ]均为空 String [] name = new String [] {"张三","李四"} ; //也可写为String [] name = {"张三","李四"};
②Dynamic initialization:
后面的[ ]必须带数字规定长度,并且不能{}赋值 String [ ] name ; name = new String [2] ; //可以合为一步写String name [] = new String [2]; /**********错误示范****错误示范****错误示范********/ int ids [3][4] = XXXXXXXXX; int ids [3][ ] = XXXXXXXXX; int ids [ ][4] = XXXXXXXXX;
int arr1 [][] =XXXXXXXXXXX; int [] arr2 []=XXXXXXXXXXX; int [][] arr3 =XXXXXXXXXXX; //都是正确写法 Java主张 int [] arr = new int []{}; 写法 //多维数组同理 int [] arr[][] =XXXXXXXX; int [][] arr[] =XXXXXXXX; 随便瞎基8放都是正确的 Java主张 int [][] arr = new int [][]{};写法 不过我个人不习惯。。。
int arr0 [][] = {{1,2},{3,4},{5,6}};//正确:静态、自动类型推断 int arr1 [][] = new int [3][2]; int arr2 [][] = new int [3][ ];//正确:动态、可省列数[] int arr3 [][][] = new int [3][ ][ ];//正确:动态、可省后两个[]
Default value problem: Dynamic [i][j] has a default value, static {} has no default value (detailed explanation at the end)
int arr0 [][] = new int [2][2];//动态初始化,分配内存 //数组内的4个成员全部默认为0,编译运行都不会报错 int arr0 [][] = new int [][]{ {1,2},{3} }; //静态初始化,编译能通过,arr0[1][1]没有默认值(未分配空间) //编译能通过,运行到arr[1][1]会报错 int arr0[][] = new int [3][] ; // 输出arr0[3][0] arr0[3][1] arr0[3][2] arr0[3][3]....... //都会报错Error case:
The array subscript is out of bounds: arr[1][1] is not given a value during static initialization, so no memory space is allocated. The compilation can pass, but the access Sometimes an error will be reported. The range of the statically initialized array is limited to arr[0][0] arr[0][1] to arr[1][0]
In-depth memory understanding: (detailed explanation at the end) String cod [][] = new String [3][ ];//Only rows are defined, the number of columns is unknown System.out.println(cod[1][1]);//Columns are not defined, no Give default value. Running error
① int [ ] arr1 = new int [ ] ; //Static forgetting {}
② int [3] arr2 = new int [3] ; [5] {1,2,3,4,5} ; //Dynamic cannot contain { }
Once the array is determined (declared initialized), the length is fixed and cannot be changedThe memory allocation of the array is continuous. The system must allocate a fixed space for the array. If the 100 spaces are later occupied by other content, then the array can only use the first 99 spaces. If the length can be changed, then arr [100] Changing other content will produce bugs
An example illustrates the array length problem:Two-dimensional array traversal, two-layer for loop:public class Test2 { public static void main(String args[]) { int arr[][] = { {1,2,3},{4,5,6,7},{8,9},{10}}; for(int i = 0 ; i <arr.length ; i++){//arr.length for(int j = 0 ; j<arr[i].length ;j++){//arr[i].length System.out.println(arr[i][j]+" "); } System.out.println(); } //System.out.println(arr[2][2]);报错下标越界 } }Running results:
arr.length is the length of the first dimension
arr[i].length is the length of the second dimension of row i
arr [i][j].length is the length of the third dimension of row i and column jArray memory analysis:Take a two-dimensional array as an example, divided into inner and outer layers . The shape of arr[0] arr[1] is the outer layer, and the complete expression of arr[1][2] is the inner layer 1. For an outer layer arr[1], it is an One-dimensional arrays will be divided into two situations: "initialized" and "uninitialized": Example 1:int arr1[][] = new int [3][]; System.out.println( arr1[1] ); //运行结果:nullAt this time, only the number of rows in the outer layer is known, but not in the inner layer. Initialization, no address is allocated, so the result is null
int arr1[][] = new int [3][5];//任意给定列数 System.out.println(arr1[1]); //运行结果:[I@15db9742At this time, the inner length is determined, initialization is completed, arr1[1] points to a first address [I@15db9742a [ represents arr[ 1] The inner layer is a one-dimensional array, I is the first letter of int, @ is followed by the address Example 2:
String arr1[][] = new String [3][5]; System.out.println(arr1); //运行结果:[[Ljava.lang.String;@15db9742Because the String array is completely defined, space is allocated, arr1 Points to the address of arr1[0][0] (the first address of the entire array)
两个[[表示里面有二维数组,java.lang.String;是包名
例3:
float arr1[][] = new float [3][5]; System.out.println(arr1[0]); //结果:[F@15db9742
外层元素初始化值为:地址值
内层元素初始化值为:与一维数组初始化情况相同
一维数组初始化:
1.数组元素是整形int long short : 0
2.数组元素是浮点型float double : 0.0
3.数组元素是boolean型:false
4.数组元素是引用类型 : null
5.数组元素是char型 :0 或 '\u0000' 而非‘0’
数字0,而不是字符0,对应的ASCII码不一样
'\u0000'是Unicode码第0000号位,是一个空格符,但是不是键盘上的空格
验证如下:
經典例題:
解:b e可以,acdf都不行
推荐学习:《java视频教程》
The above is the detailed content of Detailed explanation of arrays in Java summary. For more information, please follow other related articles on the PHP Chinese website!