I am going through the Oracle tutorials on Java and there is one thing I do not understand so far. This is why the outcome of the arrays is what is shown.
The code is:
public static void main (String [] args){
String[][] names = {
{"Mr. ", "Mrs. ", "Ms. "},
{"Smith", "Jones"}
};
// Mr. Smith
System.out.println(names[0][0] +
names[1][0]);
// Ms. Jones
System.out.println(names[0][2] +
names[1][1]);
}
I understand that Mr gets printed out as the index is 0. But smith gets printed out when it is [1][0], and also I understand how Ms is printed out because the index is 2. But I do not understand how Jones is printed out.
What I don’t understand is how the multidimensional arrays use the indexes.
Please correct me if I am wrong but do multidimensional arrays work like rows? So the first array would be 0 and the next array would be 1 and so on?
Which is why [1] which is the second array and [0] is equal to “Smith” because it is the index 0?
The first index of
namesselects theStringarray to pick from, while the second index is the element of that array that is picked.Just as
"Smith"islastNames[0], which isnames[1][0],"Jones"islastNames[1], which is the same asnames[1][1].This is correct.