Answer the question
In order to leave comments, you need to log in
Java multidimensional array - how to explain string?
Wrote a code that works :)
But there was a question with a string in a nested loop.
Here is the whole working code:
public class Main{
public static void main(String args[]){
int a[][] = {{1,2,3,4,5,6},{1,2,3,4,5,6},{1,2,5,6},{1,2,3,4,5,6}};
for(int i = 0; i < a.length; i++){
for(int k = 0; k < a[i].length; k++){
System.out.print(a[i][k]);
}
System.out.println("");
}
}
}
for(int k = 0; k < a[i].length; k++)
Answer the question
In order to leave comments, you need to log in
I can't understand what's confusing you. In the loop over i, we iterate over arrays - the elements of the array a. In a loop over k, we iterate over the elements of these same arrays. That is, we display the elements of all arrays in turn.
The loop runs as long as the condition is true. Your condition is written correctly, and it will be true until k < a[i].length
At the first iteration
k == 0
a[i].length == 6
What confused you? int a[][] = {
{1,2,3,4,5,6},
{1,2,3,4,5,6},
{1,2,5,6},
{1,2,3,4,5,6}
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question