Mục lục bài viết
Cập Nhật: 2021-12-31 21:22:32,Quý quý khách Cần tương hỗ về Initialize 2D list Java. You trọn vẹn có thể lại Comment ở phía dưới để Ad được tương hỗ.
This post will discuss how to declare and initialize two-dimensional arrays in Java.
Tóm lược đại ý quan trọng trong bài
The most common way to declare and initialize two-dimensional arrays in Java is using shortcut syntax with array initializer:
123456int[][] arr =1, 2, 3,4, 3, 6,7, 8, 9;
We can also declare and initialize two-dimensional arrays by using a new operator, as shown below:
12int[][] arr;// declare arrayarr = new int[3][4];// allocate memory
Since we have not provided any initializer, the default value of 0 is assigned to each element in the case of int or long or short or byte array. The default value is null for strings, and for double or float, the default value is 0.0.
We can combine declaration and initialization of two-dimensional arrays in a single line:
1int[][] arr = new int[3][4];
The second dimension in a two-dimensional array is optional, and we can declare a two-dimensional array by only specifying the first dimension, as shown below:
12345int[][] arr = new int[3][];arr[0] = new int[4];arr[1] = new int[4];arr[2] = new int[4];
Please note that we must specify the first dimension, otherwise the compiler will throw a compilation error.
A jagged array, also known as array of arrays, is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes.
We know that a two-dimensional array is nothing but an array of one-dimensional arrays. Therefore, it is possible to create a two-dimensional array in Java, where individual one-dimensional arrays have different lengths. To illustrate, consider the following example.
12345int[][] arr = new int[3][];arr[0] = new int[1];arr[1] = new int[2];arr[2] = new int[3];
This will create a two-dimensional array, as shown below:
[0]
[0, 0]
[0, 0, 0]
We can also initialize columns of different length with an array initializer, as shown below:
123456int[][] arr = 1 , 1, 2 , 1, 2, 3 ;
or
12345int[][] arr = new int[3][];arr[0] = new int[] 1 ;arr[1] = new int[] 1, 2 ;arr[2] = new int[] 1, 2, 3 ;
The above codes will result in:
[1]
[1, 2]
[1, 2, 3]
We can also use reflection to create two-dimensional arrays with the specified type and dimensions. For example, to create a 3 × 4 two-dimensional array, we can use
1int[][] arr = (int[][]) Array.newInstance(int.class, 3, 4);
To initialize a two-dimensional array of characters, we can use String.toCharArray() method, as shown below:
123456char[][] ch =“ABCD”.toCharArray(),“EF”.toCharArray(),“GHI”.toCharArray();
This is equivalent to:
123456char[][] ch =‘A’, ‘B’, ‘C’, ‘D’,‘E’, ‘F’,‘G’, ‘H’, ‘I’;
We can initialize a two-dimensional array of objects using new Type(), as shown below:
12345678Foo[][] array = new Foo(), new Foo(), new Foo() , new Foo(), new Foo(), new Foo() , new Foo(), new Foo(), new Foo()
Alternately, we can initialize it with nulls using the following syntax:
1Foo[][] array = new Foo[3][4];
Thats all about declaring and initializing two-dimensional arrays in Java.
Reply
6
0
Chia sẻ
– Một số Keywords tìm kiếm nhiều : ” Video full hướng dẫn Initialize 2D list Java tiên tiến và phát triển nhất , Chia Sẻ Link Download Initialize 2D list Java “.
Quý quý khách trọn vẹn có thể để lại Comments nếu gặp yếu tố chưa hiểu nhé.
#Initialize #list #Java