|
| 1 | +# Java Tutorial: Multidimensional Arrays in Java |
| 2 | +- Multidimensional Arrays are an Array of Arrays. Each elements of an M-D array is an array itself. Marks in the previous example was a 1-D array. |
| 3 | + |
| 4 | +### Multidimensional 2-D Array |
| 5 | +- A 2-D array can be created as follows: |
| 6 | + |
| 7 | + int [][] flats = new int[2][3] //A 2-D array of 2 rows + 3 columns |
| 8 | + |
| 9 | +- We can add elements to this array as follows |
| 10 | + |
| 11 | + flats[0][0] = 100 |
| 12 | + flats[0][1] = 101 |
| 13 | + flats[0][2] = 102 |
| 14 | + // … & so on! |
| 15 | + |
| 16 | +- This 2-D array can be visualized as follows: |
| 17 | + |
| 18 | +<img src="https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-28/base64.png" alt=""> |
| 19 | + |
| 20 | +- Similarly, a 3-D array can be created as follows: |
| 21 | + |
| 22 | + String[][][] arr = new String [2][3][4] |
| 23 | + |
| 24 | +### Code as Described in the Video |
| 25 | + |
| 26 | +``` |
| 27 | +package com.company; |
| 28 | +
|
| 29 | +public class cwh_28_multi_dim_arrays { |
| 30 | + public static void main(String[] args) { |
| 31 | + int [] marks; // A 1-D Array |
| 32 | + int [][] flats; // A 2-D Array |
| 33 | + flats = new int [2][3]; |
| 34 | + flats[0][0] = 101; |
| 35 | + flats[0][1] = 102; |
| 36 | + flats[0][2] = 103; |
| 37 | + flats[1][0] = 201; |
| 38 | + flats[1][1] = 202; |
| 39 | + flats[1][2] = 203; |
| 40 | +
|
| 41 | + // Displaying the 2-D Array (for loop) |
| 42 | + System.out.println("Printing a 2-D array using for loop"); |
| 43 | + for(int i=0;i<flats.length;i++){ |
| 44 | + for(int j=0;j<flats[i].length;j++) { |
| 45 | + System.out.print(flats[i][j]); |
| 46 | + System.out.print(" "); |
| 47 | + } |
| 48 | + System.out.println(""); |
| 49 | + } |
| 50 | +
|
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +**Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-28/Chapter_6_Arrays.pdf)** |
| 56 | + |
| 57 | +**Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-28/UltimateJavaCheatSheet.pdf)** |
0 commit comments