Search⌘ K
AI Features

Array Examples: Part 2

Explore how to work with multidimensional arrays in Java, including declaration and initialization. Understand how to manipulate arrays using built-in properties, methods like arraycopy, for loops, and copyOfRange to efficiently copy array elements.

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:

Java
/*
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 ...