Java के Multi-dimensional Arrays
In this page:
dataType[][] arrayName = new dataType[rows][columns];
dataType[][] arrayName = {{a, b}, {c, d}};
arrayName[row][column]
Declaring 2D Arrays
एक two-dimensional array असल में एक array है जिसके elements खुद arrays हैं, जो है कैसे Java grid-like data represent करता है जैसे एक spreadsheet, एक game board, या linear algebra में एक matrix।
आप int[][] grid = new int[rows][cols]; से एक declare करते हैं।
उदाहरण: Declaring 2D Arrays
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
int[][] grid = new int[2][3];
grid[0][0] = 1;
// Print a line to the console
System.out.println(grid[0][0]);
}
}
Login to try C/C++/Java/PHP code in the editor
Accessing 2D Array Elements
एक specific cell तक पहुंचने के लिए, पहले row index करें और फिर column, जैसे row 2, column 3 के लिए grid[2][3] — order mix up करना subtle bugs का एक आम source है जब array square न हो। दोनों indices अब भी zero-based हैं, एक normal array की तरह।
उदाहरण: Accessing 2D Array Elements
public class Main {
public static void main(String[] args) {
int[][] grid = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
System.out.println(grid[2][3]); // row 2, column 3
}
}
Login to try C/C++/Java/PHP code in the editor
Iterating 2D Arrays
Nested loops एक 2D array में चलने का standard तरीका हैं: outer loop row index आगे बढ़ाता है, और inner loop उस row के अंदर column index आगे बढ़ाता है, हर cell को बिल्कुल एक बार visit करते हुए।
यह pattern एक और nested loop जोड़कर सीधे 3D arrays तक generalize होता है।
उदाहरण: Iterating 2D Arrays
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
int[][] grid = {{1, 2}, {3, 4}};
// Loop: repeat while the condition holds
for (int row = 0; row < grid.length; row++) {
// Loop: repeat while the condition holds
for (int col = 0; col < grid[row].length; col++) {
// Print to the console without a newline
System.out.print(grid[row][col] + " ");
}
}
}
}
Login to try C/C++/Java/PHP code in the editor
Jagged Arrays
एक jagged array एक 2D array है जहां हर row एक अलग length हो सकती है — आप इसे सिर्फ row dimension fixed के साथ declare करते हैं (int[][] jagged = new int[3][];) और फिर हर row की array को अलग से उस size के साथ allocate करते हैं जिसकी उस row को ज़रूरत है।
यह तब उपयोगी है जब आपका data genuinely rectangular नहीं है, जैसे varying word counts वाले sentences की एक list।
उदाहरण: Jagged Arrays
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
int[][] jagged = new int[3][];
jagged[0] = new int[]{1};
jagged[1] = new int[]{1, 2};
jagged[2] = new int[]{1, 2, 3};
// Print a line to the console
System.out.println(jagged[2].length);
}
}
Login to try C/C++/Java/PHP code in the editor
3D Arrays
एक 3D array वही idea एक level आगे extend करता है, effectively 2D arrays का एक array, volumetric data represent करने के लिए उपयोगी जैसे एक Rubik's cube state या एक simulation में एक 3D grid।
हर additional dimension total उपयोग की गई memory को multiply करता है, इसलिए बड़े 3D arrays जल्दी महंगे हो सकते हैं।
उदाहरण: 3D Arrays
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
int[][][] cube = new int[2][2][2];
cube[0][1][1] = 5;
// Print a line to the console
System.out.println(cube[0][1][1]);
}
}
Login to try C/C++/Java/PHP code in the editor
- Rows और columns को confuse करना, जैसे column count के लिए
arr[0].lengthके बजायarr.lengthउपयोग करना। - Second size दिए बिना एक 2D array बनाना और फिर एक row access करना, जो
nullहै। - यह मान लेना कि सभी rows की length वही है, जब jagged arrays अलग हो सकते हैं।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: