Multi-Dimensional Arrays

Submitted by: 

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"

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


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.