🧮
Basics

2D Arrays

Grid of Boxes
💡 2D array एक seat-chart (rows और columns) जैसा है — जैसे class में rows और seats, हर seat का अपना row-column address होता है।

2D array एक "array of arrays" है — पहला index row बताता है, दूसरा column।

इससे grid-जैसा data store होता है — जैसे tic-tac-toe board, या matrix।

int[][] grid = {
  {1, 2, 3},
  {4, 5, 6}
};
System.out.println(grid[1][2]); // 6 (row 1, col 2)
🧮
2D array एक seat-chart (rows और columns) जैसा है — जैसे class में rows और seats, हर seat का अपना row-column address होता है।
1 / 6
⚡ झट से Recap
  • 2D array = array of arrays
  • grid[row][col] से access
  • Matrix/grid data के लिए best
इस page में (4 subtopics)

Java में 2D array actually "array of arrays" है (ना कि एक continuous grid, जैसा C में होता है) — इसी वजह से हर row की length अलग हो सकती है — इसे "jagged array" कहते हैं (जैसे एक triangle shape बनती है)। ये normal rectangular grid से ज़्यादा flexible है, जब data naturally uneven हो (जैसे एक class में हर student के अलग number of subjects)।

int[][] jagged = new int[3][];
jagged[0] = new int[]{1};
jagged[1] = new int[]{1, 2};
jagged[2] = new int[]{1, 2, 3};

2D array traverse करने के दो common तरीक़े: nested for loop (index के साथ, जब position भी चाहिए — जैसे grid[i][j] modify करना), या nested for-each (जब सिर्फ़ values चाहिए, simpler code, लेकिन index नहीं मिलता)।

for (int[] row : grid) {
  for (int val : row) {
    System.out.print(val + " ");
  }
}

Java 3D (int[][][]) या उससे ज़्यादा dimensions भी support करता है — "array of array of arrays"। Practical use कम होता है (mostly scientific computing, 3D grids/voxels), 2D ही सबसे common है real projects में। Concept same रहता है — बस एक और level की nesting।

2D arrays का use होता है: matrix operations (addition, multiplication — linear algebra), game boards (chess, tic-tac-toe — हर cell एक position), image data (pixels rows/columns में, हर cell एक color value), और spreadsheet-जैसा tabular data store करने के लिए जहाँ rows और columns दोनों meaningful हों।