...

/

Array Examples: Part 2

Array Examples: Part 2

Understand the concept of arrays with the help of the examples in this lesson.

Coding example: 32

You may consider a situation where we can put arrays inside an array. It may be complex, but accessing multidimensional array elements is not difficult. The following example shows us the declaration, initialization, and creation of a multidimensional array:

Press + to interact
/*
In multidimensional array components are themselves arrays
The rows can vary in length
*/
public class ExampleThirtyTwo {
public static void main(String[] args){
//you may imagine it as columns and rows
String[][] nameCollections = {
{"Name", "Location", "Occupation"}, //[0][0] => Name
{"John Smith", "Chicago", "Gunner"}, //[1][0] => John ...
{"Ernest Hemingway", "Writer"}, //[2][0] => Ernest...
{"Don Juan", "Paris", "Artist"} //[3][0] => Don...
};
//the first column name represents the first index as [0][0], and moves on
System.out.println(nameCollections[0][0] + " : " + nameCollections[1][0]);
System.out.println(nameCollections[0][1] + " : " + nameCollections[1][1]);
System.out.println(nameCollections[0][2] + " : " + nameCollections[1][2]);
System.out.println("+++++++++++++++");
System.out.println(nameCollections[0][0] + " : " + nameCollections[2][0]);
System.out.println(nameCollections[0][2] + " : " + nameCollections[2][1]);
System.out.println("+++++++++++++++");
System.out.println(nameCollections[0][0] + " : " + nameCollections[3][0]);
System.out.println(nameCollections[0][2] + " : " + nameCollections[3][1]);
System.out.println(nameCollections[0][2] + " : " + nameCollections[3][2]);
}
}

Code explanation

The output of this multidimensional array will help you understand how it actually works. We can also ...