← Back to Core Java Course | Chapter 5: Methods & Arrays | Lesson 9 of 10

Java के Multi-dimensional Arrays

एक multi-dimensional array rows और columns वाली values की एक table है, एक chessboard या एक spreadsheet जैसा। आप दो numbers, row और column उपयोग करके एक item ढूंढते हैं।
Syntax
java
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

java
// 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]);
	}
}

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

java
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
	}
}

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

java
// 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] + " ");
			}
		}
	}
}

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

java
// 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);
	}
}

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

java
// 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]);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Rows और columns को confuse करना, जैसे column count के लिए arr[0].length के बजाय arr.length उपयोग करना।
  2. Second size दिए बिना एक 2D array बनाना और फिर एक row access करना, जो null है।
  3. यह मान लेना कि सभी rows की length वही है, जब jagged arrays अलग हो सकते हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.