Skip to main content

Multi-Dimensional Arrays


Introduction:
Welcome to the Java tutorial on Multi-Dimensional Arrays. I would highly recommend you have a look at my Arrays page before reading through this one!

What is a Multi-Dimensional Array?
A Multi-Dimensional Array is an array which contains more than one index at any given placement. For example; instead of just having a value at the index "3", we could have "3""0", "3""1" etc.

When are Multi-Dimensional Arrays Used?
Compared to arrays there are not used as often. A good purpose for Multi-Dimensional Arrays is when you have sub-topics or values of main topics or values. So for example we could have some titles or books and games, like shown below (theory):
(0 = Books, 1 = Games)
[0][0] = "Book A"
[0][1] = "Book B"
[0][2] = "Book C"
[1][0] = "Game A"
[1][1] = "Game B"
[1][2] = "Game C"

Examples:
Here are some code samples to help you understand:

  1. String[][] myArray = new String[10][10];
  2. myArray[0][0] = "Book A";
  3. myArray[0][1] = "Book B";
  4. myArray[0][2] = "Book C";
  5. myArray[1][0] = "Game A";
  6. myArray[1][1] = "Game B";
  7. myArray[1][2] = "Game C";
  8. System.out.println(myArray[0][2]);
  9. System.out.println(myArray[1][1]);
  10. String[][] presetArray = {{"Book A", "Book B", "Book C"}, {"Game A", "Game B", "Game C"}};
  11. System.out.println(presetArray[1][2]);

The above code outputs the following...

Book C
Game B
Game C

Finished!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.