簡體   English   中英

Java中創建二維數組的語法

[英]Syntax for creating a two-dimensional array in Java

考慮:

int[][] multD = new int[5][];
multD[0] = new int[10];

這就是創建 5 行 10 列的二維數組的方式嗎?

我在網上看到這段代碼,但是語法沒有意義。

請嘗試以下操作:

int[][] multi = new int[5][10];

...這是這樣的事情的簡寫:

int[][] multi = new int[5][];
multi[0] = new int[10];
multi[1] = new int[10];
multi[2] = new int[10];
multi[3] = new int[10];
multi[4] = new int[10];

請注意,每個元素都將初始化為int0的默認值,因此上述內容也等效於:

int[][] multi = new int[][]{
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};

我們可以聲明一個二維數組,並在聲明時直接存儲元素為:

int marks[][]={{50,60,55,67,70},{62,65,70,70,81},{72,66,77,80,69}};

這里int表示存儲在數組中的整數類型元素,數組名稱為 'marks'。 int是“{”和“}”大括號內表示的所有元素的數據類型,因為數組是具有相同數據類型的元素的集合。

回到我們上面寫的語句:每一行元素都應該寫在花括號內。 行和每行中的元素應該用逗號分隔。

現在觀察語句:您可以得到 3 行 5 列,因此 JVM 創建 3 * 5 = 15 個內存塊。 這些塊可以單獨稱為:

marks[0][0]  marks[0][1]  marks[0][2]  marks[0][3]  marks[0][4]
marks[1][0]  marks[1][1]  marks[1][2]  marks[1][3]  marks[1][4]
marks[2][0]  marks[2][1]  marks[2][2]  marks[2][3]  marks[2][4]


筆記:
如果要存儲n 個元素,則數組索引從零開始並以n-1結束。 創建二維數組的另一種方法是先聲明數組,然后使用 new 運算符為其分配內存。

int marks[][];           // declare marks array
marks = new int[3][5];   // allocate memory for storing 15 elements

結合以上兩者,我們可以寫出:

int marks[][] = new int[3][5];

您可以按照其他人提到的方式創建它們。 還要補充一點:您甚至可以為每一行創建一個傾斜的二維數組,不一定具有相同數量的列,如下所示:

int array[][] = new int[3][];
array[0] = new int[3];
array[1] = new int[2];
array[2] = new int[5];

創建具有510列的二維數組的最常見習慣用法是:

int[][] multD = new int[5][10];

或者,您可以使用以下內容,這與您擁有的內容更相似,但您需要顯式初始化每一行:

int[][] multD = new int[5][];
for (int i = 0; i < 5; i++) {
  multD[i] = new int[10];
}

也可以通過以下方式聲明它。 這不是好的設計,但它有效。

int[] twoDimIntArray[] = new int[5][10];

嘗試:

int[][] multD = new int[5][10];

請注意,在您的代碼中,只有二維數組的第一行被初始化為 0。第 2 行到第 5 行甚至不存在。 如果您嘗試打印它們,那么每個人都會得到null

在 Java 中,可以將二維數組聲明為與一維數組相同的聲明。 在一個一維數組中,你可以這樣寫

  int array[] = new int[5];

其中 int 是一種數據類型,array[] 是一個數組聲明,而new array是一個數組,其對象具有五個索引。

像這樣,您可以編寫一個二維數組,如下所示。

  int array[][];
  array = new int[3][4];

這里的array是一個 int 數據類型。 我首先聲明了該類型的一維數組,然后創建了一個 3 行 4 列數組。

在你的代碼中

int[][] multD = new int[5][];
multD[0] = new int[10];

意味着您已經創建了一個二維數組,有五行。 第一行有 10 列。 在 Java 中,您可以根據需要選擇每一行的列大小。

int [][] twoDim = new int [5][5];

int a = (twoDim.length);//5
int b = (twoDim[0].length);//5

for(int i = 0; i < a; i++){ // 1 2 3 4 5
    for(int j = 0; j <b; j++) { // 1 2 3 4 5
        int x = (i+1)*(j+1);
        twoDim[i][j] = x;
        if (x<10) {
            System.out.print(" " + x + " ");
        } else {
            System.out.print(x + " ");
        }
    }//end of for J
    System.out.println();
}//end of for i
int rows = 5;
int cols = 10;

int[] multD = new int[rows * cols];

for (int r = 0; r < rows; r++)
{
  for (int c = 0; c < cols; c++)
  {
     int index = r * cols + c;
     multD[index] = index * 2;
  }
}

享受!

試試這個方法:

int a[][] = {{1,2}, {3,4}};

int b[] = {1, 2, 3, 4};

這些類型的數組在 Java 中稱為鋸齒數組:

int[][] multD = new int[3][];
multD[0] = new int[3];
multD[1] = new int[2];
multD[2] = new int[5];

在這種情況下,數組的每一行都包含不同數量的列。 在上面的例子中,第一行將包含三列,第二行將包含兩列,第三行將包含五列。 您可以在編譯時初始化此數組,如下所示:

 int[][] multD = {{2, 4, 1}, {6, 8}, {7, 3, 6, 5, 1}};

您可以輕松迭代數組中的所有元素:

for (int i = 0; i<multD.length; i++) {
    for (int j = 0; j<multD[i].length; j++) {
        System.out.print(multD[i][j] + "\t");
    }
    System.out.println();
}

實際上,Java 沒有數學意義上的多維數組。 Java 所擁有的只是數組的數組,一個數組,其中每個元素也是一個數組。 這就是為什么初始化它的絕對要求是第一維的大小。 如果指定了其余部分,則它將創建一個填充有默認值的數組。

int[][]   ar  = new int[2][];
int[][][] ar  = new int[2][][];
int[][]   ar  = new int[2][2]; // 2x2 array with zeros

它也給了我們一個怪癖。 子數組的大小不能通過添加更多元素來改變,但我們可以通過分配一個任意大小的新數組來實現。

int[][]   ar  = new int[2][2];
ar[1][3] = 10; // index out of bound
ar[1]    = new int[] {1,2,3,4,5,6}; // works

如果你想要動態和靈活的東西(即你可以添加或刪除列和行的地方),你可以嘗試“ArrayList of ArrayList”:

public static void main(String[] args) {

    ArrayList<ArrayList<String>> arrayListOfArrayList = new ArrayList<>();

    arrayListOfArrayList.add(new ArrayList<>(List.of("First", "Second", "Third")));
    arrayListOfArrayList.add(new ArrayList<>(List.of("Fourth", "Fifth", "Sixth")));
    arrayListOfArrayList.add(new ArrayList<>(List.of("Seventh", "Eighth", "Ninth")));
    arrayListOfArrayList.add(new ArrayList<>(List.of("Tenth", "Eleventh", "Twelfth")));

    displayArrayOfArray(arrayListOfArrayList);
    addNewColumn(arrayListOfArrayList);
    displayArrayOfArray(arrayListOfArrayList);
    arrayListOfArrayList.remove(2);
    displayArrayOfArray(arrayListOfArrayList);
}

private static void displayArrayOfArray(ArrayList<ArrayList<String>> arrayListOfArrayList) {
    for (int row = 0; row < arrayListOfArrayList.size(); row++) {
        for (int col = 0; col < arrayListOfArrayList.get(row).size(); col++) {
            System.out.printf("%-10s", arrayListOfArrayList.get(row).get(col));
        }
        System.out.println("");
    }
    System.out.println("");
}

private static void addNewColumn(ArrayList<ArrayList<String>> arrayListOfArrayList) {
    for (int row = 0; row < arrayListOfArrayList.size(); row++) {
        arrayListOfArrayList.get(row).add("added" + row);
    }
}

Output:

First     Second    Third     
Fourth    Fifth     Sixth     
Seventh   Eighth    Ninth     
Tenth     Eleventh  Twelfth   

First     Second    Third     added0    
Fourth    Fifth     Sixth     added1    
Seventh   Eighth    Ninth     added2    
Tenth     Eleventh  Twelfth   added3    

First     Second    Third     added0    
Fourth    Fifth     Sixth     added1    
Tenth     Eleventh  Twelfth   added3    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM