Reputation: 21
I am in the process of learning Java and am very confused on multidimensional arrays. When I say this I don't mean the array syntax but more so the logic of using arrays with for statements. What I am wondering is how do I incorporate arrays into for statements correctly and what does all of the code in play do, and why is it there. Here is some code I have been working on (based off a tutorial) and was wondering if someone could fully explain everything that is going on.
package tutorial;
public class apples {
public static void taco(String[] args) {
int firstarray[][]={{8,9,10,11},{12,13,14,15}};
int secondarray[][]={{30,31,32,33},{43},{4,5,6}};
System.out.println("This is the first array");
display(firstarray);
System.out.println("This is the second array");
display(secondarray);
}
public static void display(int x[][]) {
for (int row=0;row<x.length;row++) {
for (int column=0;column<x[row].length;column++) {
System.out.println(x[row][column]+"\t");
}
System.out.println();
}
}
So what I don't understand is the entire
public static void display(int x[][]) {
for (int row=0;row<x.length;row++) {
for (int column=0;column<x[row].length;column++) {
System.out.println(x[row][column]+"\t");
If someone could explain that in more depth that would be great. I get for statements and arrays in general, im just confused with how this works.
Upvotes: 0
Views: 168
Reputation: 19
When you declare an array like this:
int arr[][]={{8,9,10,11},{12,13,14,15}};
You're pretty much declaring an array with 2 rows and 4 columns. Visually it makes sense to think of it like this:
{8,9,10,11}
{12,13,14,15}
Then when you try to access a certain element, think of the top leftmost element as [0][0].
So arr[0][0] would equal 8.
I find it easiest to think of multi dimensional arrays not just as rows/columns, but also as x and y. Except it's a little tricky because it's counter-intuitive in the sense the format is:
arr[y_coordinate][x_coordinate].
as you move from 8 to the right, the x coordinate increases.
as you move from 8 downwards, the y coordinate increases.
So, to access 9, you could use arr[0][1]. To access 12, you could use arr[1][0].
Now on to the method you'd like explained:
public static void display(int x[][]) {
for (int row=0;row<x.length;row++) {
for (int column=0;column<x[row].length;column++) {
System.out.println(x[row][column]+"\t");
x.length returns the number of rows, or y coordinates, your multi-d array has.
x[row].length returns the number of columns, or x coordinates, a particular row index has.
What this method does is it starts at the top y coordinate (the first row), and then it checks how many columns/x coordinates that row has. It then loops through those x coordinates in that row from left to right printing its value. When it has finished printing all the values in a row, it moves on to the next row!
On a side note, I would edit your display method ever so slightly to:
public static void display(int x[][]) {
for (int row=0;row<x.length;row++) {
for (int column=0;column<x[row].length;column++) {
System.out.print(x[row][column]+"\t");
}
System.out.println();
}
}
I changed a System.out.println to System.out.print
why? Because then it only prints a new line for the next row.
Let me know if any part of this is unclear.
Upvotes: 1
Reputation: 89
the following method
public static void display(int x[][]) {
for (int row=0;row<x.length;row++) {
for (int column=0;column<x[row].length;column++) {
System.out.println(x[row][column]+"\t");
}
}
}
is similar to the following method
public static void display(int x[][]) {
//x is an array with two dimensions, so its elements are arrays with one dimension
for (int row=0;row<x.length;row++) {
//x.length returns the count of all stored arrays in the array x
int[] rowArray_ = x[row];//x[row] is an array with one dimension
for (int column=0;column<rowArray_.length;column++) {
System.out.println(rowArray_[column]+"\t");
}
}
}
@Cole Riggle: in your code, you have:
int secondarray[][]={{30,31,32,33},{43},{4,5,6}};
so secondarray is componed of three arrays which are:
{30,31,32,33}
{43}
{4,5,6}
so in your static method, you process on the arrays in the following order
{30,31,32,33}
{43}
{4,5,6}
I hope you will understand my explanation.
If not, see multidimensional-arrays-lengths-in-java
Upvotes: 0
Reputation: 393831
You can look at a two dimensional array int x[][]
as an array whose elements are themselves arrays of int
.
Therefore, x
has x.length
int array elements, and each of them is an int array of x[row].length
ints.
Upvotes: 1