Mục lục bài viết

Mẹo về Initialize 2D list Java 2022

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ỗ.

556

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

  • 1. Shortcut Syntax
  • 2. New Operator
  • 3. Combining declaration and initialization
  • 4. Skipping second dimension
  • 5. Jagged Array
  • 6. Initialize columns with Array Initializer
  • 7. Reflection
  • 8. Initialize character array
  • 9. Initialize object array

1. Shortcut Syntax

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;

2. New Operator

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.

3. Combining declaration and initialization

We can combine declaration and initialization of two-dimensional arrays in a single line:

1int[][] arr = new int[3][4];

4. Skipping second dimension

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.

5. Jagged Array

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]

6. Initialize columns with Array Initializer

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]

7. Reflection

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);

8. Initialize character array

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’;

9. Initialize object array

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ẻ

đoạn Clip hướng dẫn Share Link Down Initialize 2D list Java ?

– 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 “.

Thảo Luận vướng mắc về 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