I want to make a loop on Two-dimensional array in Java.
How I do that? I wrote:
for (int i = 0; i<=albums.size() - 1; i++){
for (int j = 0; j<=albums.size() - 1; j++){
But it didn’t work. Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Arrays have a read-only field called
length, not a method calledsize. A corrected loop looks like this:You have to recognize that a 2-D array is just an array whose element type happens to be another array. So the
iloop iterates over each element inalbums(which is an array) and thejloop iterates over that child array (with a potentially different size).A more transparent way would be like this: