Ein mehrdimensionales Array. Die am häufigsten verwendeten mehrdimensionalen Arrays sind 2D- und 3D-Arrays. Wir können sagen, dass jedes höherdimensionale Array im Grunde ein Array von Arrays ist. Ein sehr häufiges Beispiel für ein 2D-Array ist das Schachbrett. Ein Schachbrett ist ein Gitter mit 64 quadratischen 1×1-Feldern. Auf ähnliche Weise können Sie ein 2D-Array visualisieren. In einem 2D-Array ist jedem Element eine Zeilennummer und eine Spaltennummer zugeordnet. Der Zugriff auf ein beliebiges Element des 2D-Arrays ähnelt dem Zugriff auf den Datensatz einer Excel-Datei unter Verwendung sowohl der Zeilennummer als auch der Spaltennummer. 2D-Arrays sind nützlich, wenn Sie ein Tic-Tac-Toe-Spiel oder Schach spielen oder sogar die Bildpixel speichern.

Deklarieren eines 2D-Arrays in Java:
Jedes zweidimensionale Array kann wie folgt deklariert werden:
Syntax:
Datentyp Arrayname[][]; (ODER) data_type[][] array_name;
- data_type: Da Java eine statisch typisierte Sprache ist (d. h. es erwartet, dass seine Variablen deklariert werden, bevor ihnen Werte zugewiesen werden können). Die Angabe des Datentyps bestimmt also, welche Art von Elementen akzeptiert werden. z.B. Um nur ganzzahlige Werte zu speichern, wird der Datentyp als int deklariert. array_name: Dies ist der Name, der dem 2D-Array gegeben wird. z.B. Fächer, Studenten, Früchte, Abteilung usw.
Notiz: Wir können [ ][ ] nach data_type oder [ ][ ] nach array_name schreiben, während wir das 2D-Array deklarieren.
Java
im Markdown unterstreichen
// java program showing declaration of arrays> import> java.io.*;> > class> GFG {> >public> static> void> main(String[] args)> >{> > >int>[][] integer2DArray;>// 2D integer array> >String[][] string2DArray;>// 2D String array> >double>[][] double2DArray;>// 2D double array> >boolean>[][] boolean2DArray;>// 2D boolean array> >float>[][] float2DArray;>// 2D float array> >double>[][] double2DArray;>// 2D double array> >}> }> |
>
>
Verschiedene Ansätze zur Initialisierung eines 2D-Arrays in Java:
data_type[][] array_Name = neu data_type[no_of_rows][no_of_columns];
Die Gesamtzahl der Elemente in jedem 2D-Array ist gleich (no_of_rows) * (no_of_columns).
- no_of_rows: Die Anzahl der Zeilen in einem Array. z.B. no_of_rows = 3, dann hat das Array drei Zeilen. no_of_columns: Die Anzahl der Spalten in einem Array. z.B. no_of_columns = 4, dann hat das Array vier Spalten.
Die obige Syntax der Array-Initialisierung weist allen Array-Elementen Standardwerte entsprechend dem angegebenen Datentyp zu.
Nachfolgend finden Sie die Implementierung verschiedener Ansätze zur Initialisierung von 2D-Arrays:
Ansatz 1:
Java
iPhone-Emojis auf Android-Handy
// 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>]);> >}> }> |
>
>
Notiz: Wenn Sie ein 2D-Array initialisieren, müssen Sie immer die erste Dimension (Anzahl der Zeilen) angeben, die Angabe der zweiten Dimension (Anzahl der Spalten) kann jedoch weggelassen werden.
Im folgenden Codeausschnitt haben wir die Anzahl der Spalten nicht angegeben. Der Java-Compiler ist jedoch intelligent genug, um die Größe zu manipulieren, indem er die Anzahl der Elemente in den Spalten überprüft.
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>][];> >}> }> |
>
>
Sie können über Zeilennummern und Spaltennummern auf jedes Element eines 2D-Arrays zugreifen.

Ansatz 2:
Im folgenden Codeausschnitt haben wir die Anzahl der Zeilen und Spalten nicht angegeben. Der Java-Compiler ist jedoch intelligent genug, um die Größe zu manipulieren, indem er die Anzahl der Elemente in den Zeilen und Spalten überprüft.
Java
Windows-Befehl arp
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>]);> >}> }> |
>
>Ausgabe
Fundamental Subject in Computer Engineering: Data Structures & Algorithms Fundamental Subject in Mechanical Engineering: Fluid Mechanics Fundamental Subject in Electronics Engineering: Digital Electronics>
Ansatz 3:
Darüber hinaus können wir jedes Element des Arrays separat initialisieren. Schauen Sie sich den folgenden Codeausschnitt an:
Java
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));> >}> }> |
>
>Ausgabe
scores[0][0] = 15 scores[0][1] = 23 scores[1][0] = 30 scores[1][1] = 21 Printing 2D array using Arrays.deepToString() method: [[15, 23], [30, 21]]>
Ansatz 4
Die Verwendung des oben genannten Ansatzes zur Array-Initialisierung wäre eine mühsame Aufgabe, wenn die Größe des 2D-Arrays zu groß ist. Der effizienteste Weg ist die Verwendung einer for-Schleife zum Initialisieren der Array-Elemente im Fall eines großen 2D-Arrays.
Java
a-b-Schnitt
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 for (int j = 0; 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 System.out.printf(marks[i][j] + ' '); } System.out.println(); } } }> |
>
>Ausgabe
First three rows are: 0 1 2 3 4 1 2 3 4 5 2 3 4 5 6>
Notiz: Wir können arr verwenden. length-Funktion zum Ermitteln der Größe der Zeilen (1. Dimension) und arr[0].length-Funktion zum Ermitteln der Größe der Spalten (2. Dimension).
Ansatz 5: (Gezackte Arrays)
Es kann ein bestimmtes Szenario geben, in dem jede Zeile eine unterschiedliche Anzahl von Spalten haben soll. Diese Art von Array wird als Jagged Array bezeichnet.
Java
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 // 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][j] = count++; } } // printing the values of 2D Jagged array System.out.println('The values of 2D jagged array'); for (int i = 0; i for (int j = 0; j System.out.printf(jagged[i][j] + ' '); System.out.println(); } } }> |
>
Versuchen Sie es mit der Datenstruktur
>Ausgabe
The values of 2D jagged array 0 1 2 3 4 5>
Programm zum Hinzufügen von zwei 2D-Arrays:
Java
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 for (int j = 0; j 0].length; j++) { sum[i][j] = arr1[i][j] + arr2[i][j]; } } System.out.println('Resultant 2D array: '); for (int i = 0; i System.out.println(Arrays.toString(sum[i])); } } }> |
>
>Ausgabe
Resultant 2D array: [5, 7, 9] [5, 8, 8]>