Java 鋸齒狀陣列
本教程介紹了 Java 鋸齒狀陣列。我們將通過不同的程式碼示例瞭解它的記憶體表示和實現。
Java 鋸齒狀陣列
要理解鋸齒狀陣列,你必須對陣列有一個很好的理解。鋸齒狀陣列,也稱為不規則陣列,是一個陣列,其中每個成員陣列的大小都不同。
這裡,陣列陣列是指二維陣列;它可以是 2D 或 3D 或更多維度。請參閱以下視覺演示以瞭解鋸齒狀陣列。
我們可以看到每個成員陣列的大小互不相同。這就是我們所說的鋸齒狀或鋸齒狀陣列。
Java 中鋸齒狀陣列的宣告與初始化
有多種方式來宣告和初始化鋸齒狀陣列;我們將在下面使用從視覺解釋中提取的 int
型別陣列來檢視它們中的每一個。
使用第一種方法,我們首先通過指定大小來宣告基本陣列。然後,我們為每個成員陣列寫入不同的大小。
在這裡,我們將宣告和初始化過程分開。請參閱以下程式碼段。
宣告鋸齒狀陣列:
//declare base array of size 4 (it would be an array of arrays)
int arr[][] = new int[4][];
//declare member arrays
arr[0] = new int[3]; //here the 3 shows the no of columns in row-1
arr[1] = new int[4]; //here the 4 shows the no of columns in row-2
arr[2] = new int[1]; //here the 1 shows the no of columns in row-3
arr[3] = new int[2]; //here the 2 shows the no of columns in row-4
接下來,靜態初始化鋸齒狀陣列。
arr[0] = new int[]{1,2,3};
arr[1] = new int[]{1,2,3,4};
arr[2] = new int[]{4};
arr[3] = new int[]{4,5};
或者,我們可以動態初始化鋸齒狀陣列,這意味著我們從使用者那裡獲取輸入並在執行時初始化陣列。
Scanner sc = new Scanner(System.in);
for (int base = 0; base < arr.length; base++) {
for (int member = 0; member < arr[base].length; member++) {
arr[base][member] = sc.nextInt();
}
}
在第二種方法中,我們可以一步宣告和初始化鋸齒狀陣列。我們可以用各種方式編寫這一步。
請參閱下面給出的程式碼片段。
int arr[][] = new int[][]{
new int[] {1, 2, 3},
new int[] {1, 2, 3, 4},
new int[] {4},
new int[] {4, 5}
};
OR
int[][] arr = {
new int[] {1, 2, 3},
new int[] {1, 2, 3, 4},
new int[] {4},
new int[] {4, 5}
};
OR
int[][] arr = {
{1, 2, 3},
{1, 2, 3, 4},
{4},
{4, 5}
};
讓我們深入瞭解鋸齒狀陣列的更詳細實現,我們將在其中靜態和動態地分配值。
Java 中的鋸齒狀陣列實現示例
示例程式碼(鋸齒狀陣列是靜態初始化的):
public class jagggedArrayTest {
public static void main(String args[]) {
int arr[][] = new int[][]{
new int[] {1, 2, 3},
new int[] {1, 2, 3, 4},
new int[] {4},
new int[] {4, 5}
};
for (int base = 0; base < arr.length; base++) {
System.out.print("arr["+base+"] ======> {");
for (int member = 0; member < arr[base].length; member++) {
if(member < arr[base].length - 1)
System.out.print(arr[base][member] + ", ");
else
System.out.print(arr[base][member]);
}
System.out.print("}");
System.out.println();
}
}
}
輸出:
arr[0] ======> {1, 2, 3}
arr[1] ======> {1, 2, 3, 4}
arr[2] ======> {4}
arr[3] ======> {4, 5}
我們首先在 main
函式中宣告並初始化鋸齒狀陣列。然後,我們使用巢狀的 for
迴圈來列印鋸齒狀陣列,其中外迴圈用於迭代基本陣列(行),而內迴圈用於迭代成員陣列(列)。
示例程式碼(動態填充鋸齒狀陣列):
import java.util.Scanner;
public class jaggedArrayTest {
/*
this function prints the populated jagged array
*/
static void printJaggedArray(int[][] arr){
System.out.println("The populated array looks like as follows:");
for (int base = 0; base < arr.length; base++) {
System.out.print("arr["+base+"] ======> {");
for (int member = 0; member < arr[base].length; member++) {
if(member < arr[base].length - 1)
System.out.print(arr[base][member] + ", ");
else
System.out.print(arr[base][member]);
}
System.out.print("}");
System.out.println();
}
}
/*
this function populates the jagged array by
taking input from the user
*/
static void populateJaggedArray(int[][] arr){
Scanner sc = new Scanner(System.in);
for (int base = 0; base < arr.length; base++) {
System.out.println("Enter the member array at index " + base);
for (int member = 0; member < arr[base].length; member++) {
arr[base][member] = sc.nextInt();
}
}
//print jagged array
printJaggedArray(arr);
}
public static void main(String args[]) {
//declare base array of size 4 (it would be an array of arrays)
int arr[][] = new int[4][];
//declare member arrays
arr[0] = new int[3]; //here the 3 shows the no of columns in row-1
arr[1] = new int[4]; //here the 4 shows the no of columns in row-2
arr[2] = new int[1]; //here the 1 shows the no of columns in row-3
arr[3] = new int[2]; //here the 2 shows the no of columns in row-4
//populate jagged array
populateJaggedArray(arr);
}
}
輸出:
Enter the member array at index 0
1 2 3
Enter the member array at index 1
1 2 3 4
Enter the member array at index 2
4
Enter the member array at index 3
4 5
The populated array looks like as follows:
arr[0] ======> {1, 2, 3}
arr[1] ======> {1, 2, 3, 4}
arr[2] ======> {4}
arr[3] ======> {4, 5}
在這裡,我們在 jaggedArrayTest
類中擁有三個名為 main()
、populateJaggedArray()
和 printJaggedArray()
的方法。main()
方法宣告並初始化鋸齒狀陣列,該陣列被傳遞給 populateJaggedArray()
函式以進行填充。
此外,它呼叫 printJaggedArray()
來列印填充的鋸齒狀陣列。請記住,我們只是動態地填充鋸齒狀陣列,但你也可以使用使用者的輸入值來獲取基本陣列和成員陣列的大小。