更新時間:2022-11-25 10:04:59 來源:動力節點 瀏覽61次
data_type[][] array_Name = new data_type[no_of_rows][no_of_columns];
任何二維數組中的元素總數將等于 (no_of_rows) * (no_of_columns)。
no_of_rows:數組中的行數。例如 no_of_rows = 3,則數組將有三行。
no_of_columns:數組中的列數。例如 no_of_columns = 4,則數組將有四列。
上面的數組初始化語法會根據指定的數據類型為所有數組元素賦默認值。
下面是初始化二維數組的各種方法的實現:
// java program to initialize a 2D array
import java.io.*;
class GFG {
public static void main(String[] args)
{
// Declaration along with initialization
// 2D integer array with 5 rows and 3 columns
// integer array elements are initialized with 0
int[][] integer2DArray = new int[5][3];
System.out.println(
"Default value of int array element: "
+ integer2DArray[0][0]);
// 2D String array with 4 rows and 4 columns
// String array elements are initialized with null
String[][] string2DArray = new String[4][4];
System.out.println(
"Default value of String array element: "
+ string2DArray[0][0]);
// 2D boolean array with 3 rows and 5 columns
// boolean array elements are initialized with false
boolean[][] boolean2DArray = new boolean[4][4];
System.out.println(
"Default value of boolean array element: "
+ boolean2DArray[0][0]);
// 2D char array with 10 rows and 10 columns
// char array elements are initialized with
// '\u0000'(null character)
char[][] char2DArray = new char[10][10];
System.out.println(
"Default value of char array element: "
+ char2DArray[0][0]);
// First declaration and then initialization
int[][] arr; // declaration
// System.out.println("arr[0][0]: "+ arr[0][0]);
// The above line will throw an error, as we have
// only declared the 2D array, but not initialized
// it.
arr = new int[5][3]; // initialization
System.out.println("arr[0][0]: " + arr[0][0]);
}
}
注意:初始化二維數組時,必須始終指定第一個維度(行數),但可以省略提供第二個維度(列數)。
在下面的代碼片段中,我們沒有指定列數。然而,Java 編譯器足夠聰明,可以通過檢查列內元素的數量來操縱大小。
import java.io.*;
class GFG {
public static void main(String[] args)
{
// The line below will throw an error, as the first
// dimension(no. of rows) is not specified
int[][] arr = new int[][3];
// The line below will execute without any error, as
// the first dimension(no. of rows) is specified
int[][] arr = new int[2][];
}
}
您可以使用行號和列號訪問二維數組的任何元素。
在下面的代碼片段中,我們沒有指定行數和列數。然而,Java 編譯器足夠聰明,可以通過檢查行和列中元素的數量來操縱大小。
import java.io.*;
class GFG {
public static void main(String[] args)
{
String[][] subjects = {
{ "Data Structures & Algorithms",
"Programming & Logic", "Software Engineering",
"Theory of Computation" }, // row 1
{ "Thermodynamics", "Metallurgy",
"Machine Drawing",
"Fluid Mechanics" }, // row2
{ "Signals and Systems", "Digital Electronics",
"Power Electronics" } // row3
};
System.out.println(
"Fundamental Subject in Computer Engineering: "
+ subjects[0][0]);
System.out.println(
"Fundamental Subject in Mechanical Engineering: "
+ subjects[1][3]);
System.out.println(
"Fundamental Subject in Electronics Engineering: "
+ subjects[2][1]);
}
}
輸出
計算機工程基礎學科:數據結構與算法
機械工程基礎學科:流體力學
電子工程基礎學科:數字電子學
此外,我們可以分別初始化數組的每個元素。請看下面的代碼片段:
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
int[][] scores = new int[2][2];
// Initializing array element at position[0][0],
// i.e. 0th row and 0th column
scores[0][0] = 15;
// Initializing array element at position[0][1],
// i.e. 0th row and 1st column
scores[0][1] = 23;
// Initializing array element at position[1][0],
// i.e. 1st row and 0th column
scores[1][0] = 30;
// Initializing array element at position[1][1],
// i.e. 1st row and 1st column
scores[1][1] = 21;
// printing the array elements individually
System.out.println("scores[0][0] = "
+ scores[0][0]);
System.out.println("scores[0][1] = "
+ scores[0][1]);
System.out.println("scores[1][0] = "
+ scores[1][0]);
System.out.println("scores[1][1] = "
+ scores[1][1]);
// printing 2D array using Arrays.deepToString() method
System.out.println(
"Printing 2D array using Arrays.deepToString() method: ");
System.out.println(Arrays.deepToString(scores));
}
}
輸出
分數[0][0] = 15
分數[0][1] = 23
分數[1][0] = 30
分數[1][1] = 21
使用 Arrays.deepToString() 方法打印二維數組:
[[15, 23], [30, 21]]
如果二維數組的大小太大,使用上述方法進行數組初始化將是一項繁瑣的任務。在大型二維數組的情況下,有效的方法是使用 for 循環來初始化數組元素。
import java.io.*;
class GFG {
public static void main(String[] args)
{
int rows = 80, columns = 5;
int[][] marks = new int[rows][columns];
// initializing the array elements using for loop
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
marks[i][j] = i + j;
}
}
// printing the first three rows of marks array
System.out.println("First three rows are: ");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < columns; j++) {
System.out.printf(marks[i][j] + " ");
}
System.out.println();
}
}
}
輸出
前三行是:
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
注意:我們可以使用 arr. length 函數用于查找行的大小(第一維),arr[0].length 函數用于查找列的大小(第二維)。
在某些情況下,您可能希望每一行都有不同數量的列。這種類型的數組稱為Jagged Array。
import java.io.*;
class GFG {
public static void main(String[] args)
{
// declaring a 2D array with 2 rows
int jagged[][] = new int[2][];
// not specifying the 2nd dimension,
// and making it as jagged array
// first row has 2 columns
jagged[0] = new int[2];
// second row has 4 columns
jagged[1] = new int[4];
// Initializing the array
int count = 0;
for (int i = 0; i < jagged.length; i++) {
// remember to use jagged[i].length instead of
// jagged[0].length, since every row has
// different number of columns
for (int j = 0; j < jagged[i].length; j++) {
jagged[i][j] = count++;
}
}
// printing the values of 2D Jagged array
System.out.println("The values of 2D jagged array");
for (int i = 0; i < jagged.length; i++) {
for (int j = 0; j < jagged[i].length; j++)
System.out.printf(jagged[i][j] + " ");
System.out.println();
}
}
}
輸出
二維交錯數組的值
0 1
2 3 4 5
添加兩個二維數組的程序:
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
int[][] arr1 = { { 1, 2, 3 }, { 4, 5, 6 } };
int[][] arr2 = { { 4, 5, 6 }, { 1, 3, 2 } };
int[][] sum = new int[2][3];
// adding two 2D arrays element-wise
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr1[0].length; j++) {
sum[i][j] = arr1[i][j] + arr2[i][j];
}
}
System.out.println("Resultant 2D array: ");
for (int i = 0; i < sum.length; i++) {
System.out.println(Arrays.toString(sum[i]));
}
}
}
輸出
結果二維數組:
[5, 7, 9]
[5, 8, 8]
提交申請后,顧問老師會電話與您溝通安排學習