bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Java/Java Tutorial
Java•Java Tutorial

Java Multi-Dimensional Arrays

Multidimensional Arrays

A multidimensional array is an array that contains other arrays.

You can use it to store data in a table with rows and columns.

To create a two-dimensional array, write each row inside its own curly braces :

int[][] myNumbers = { {1, 4, 2}, {3, 6, 8} };

Here, myNumbers has two arrays (two rows):

  • First row: {1, 4, 2}
  • Second row: {3, 6, 8}

Access Elements

To access an element of a two-dimensional array, you need two indexes: the first for the row, and the second for the column.

Remember

Array indexes start at 0. That means row 0 is the first row, and column 0 is the first column. (So row index 1 is the second row, and column index 2 is the third column.)

This statement accesses the element in the second row (index 1 ) and third column (index 2 ) of the myNumbers array:

Example

int[][] myNumbers = { {1, 4, 2}, {3, 6, 8} };
System.out.println(myNumbers[1][2]); // Outputs 8

This example prints the value at row 0 , column 1 :

Example

int[][] myNumbers = { {1, 4, 2}, {3, 6, 8} };
System.out.println(myNumbers[0][1]); // Outputs 4

Change Element Values

You can overwrite an existing element using the same two-index notation (row, then column):

Example

int[][] myNumbers = { {1, 4, 2}, {3, 6, 8} };
myNumbers[1][2] = 9;
System.out.println(myNumbers[1][2]); // Outputs 9 instead of 8

Rows and Columns (Lengths)

You can use length to get the number of rows, and myNumbers[row] .length for the number of columns in a given row:

Example: Sizes

int[][] myNumbers = { {1, 4, 2}, {3, 6, 8, 5, 2} };
System.out.println("Rows: " + myNumbers.length);             // 2
System.out.println("Cols in row 0: " + myNumbers[0].length); // 3
System.out.println("Cols in row 1: " + myNumbers[1].length); // 5

Note

Notice how rows can have different lengths - In this example, the second row has more elements than the first, and that's perfectly valid in Java.

Loop Through a Multidimensional Array

Use a for loop inside another for loop to visit every element (row by row):

Example

int[][] myNumbers = { {1, 4, 2}, {3, 6, 8, 5, 2} };
for (int row = 0; row < myNumbers.length; row++) {
  for (int col = 0; col < myNumbers[row].length; col++) {
    System.out.println("myNumbers[" + row + "][" + col + "] = " + myNumbers[row][col]);
  }
}

Or use a for-each loop in both levels, which many find easier to read:

Example

int[][] myNumbers = { {1, 4, 2}, {3, 6, 8, 5, 2} };
for (int[] row : myNumbers) {
  for (int num : row) {
    System.out.println(num);
  }
}

Previous

Java Arrays Loop

Next chapter

Java Methods

Start with Java Methods

This chapter

Overview
49

Lessons

110m

Read time

1. Java Tutorial2. Java Introduction3. Java Getting Started4. Java Syntax5. Java Statements6. Java Output / Print7. Java Output Numbers8. Java Comments9. Java Variables10. Java Print Variables11. Java Declare Multiple Variables12. Java Identifiers13. Java Constants (final)14. Java Data Types15. Java Numbers16. Java Boolean Data Types17. Java Characters18. Java Data Types Example19. Java Non-Primitive Data Types20. Java var Keyword21. Java Type Casting22. Java Operators23. Java Arithmetic Operators24. Java Assignment Operators25. Java Comparison Operators26. Java Logical Operators27. Java Operator Precedence28. Java Strings29. Java String Concatenation30. Java Numbers and Strings31. Java Special Characters32. Java Math33. Java Booleans34. Java If ... Else35. Java Else36. Java Else If37. Java Short Hand If...Else (Ternary Operator)38. Java Nested If39. Java Logical Operators in Conditions40. Java Switch41. Java While Loop42. Java Do/While Loop43. Java For Loop44. Java Nested Loops45. Java For Each Loop46. Java Break and Continue47. Java Arrays48. Java Arrays Loop49. Java Multi-Dimensional Arrays

On this page

Multidimensional ArraysAccess ElementsChange Element ValuesRows and Columns (Lengths)Loop Through a Multidimensional Array