+ 1

(SOLVED) Help with Multidimensional array practice

I have really bad ADHD and I can't really write all this code properly, I'm trying to reference arrPosition to the first part of the nested for loop. I had a feeling it wouldn't work because you cant call something that's initialized later. However, I have no clue how to work around it and there's too much going on for me to process it all. Im trying to print every position in the multidimensional array, I've done this before but haven't done it since my Java class last year, Please help public class Main { public static void main(String[] args) { int[][] matrix = { {8, 1, 6}, {3, 5, 7}, {4, 9, 0}, }; //output the numbers for(int arrRow=0;arrRow<=matrix[arrRow][arrPosition].length-1;arrRow++) { for(int arrPosition=0;arrPosition!=matrix[arrRow][arrPosition].length-1;arrPosition++) { System.out.println(matrix[arrRow][arrPosition]); } } } }

25th Jan 2021, 7:14 PM
Serana Zentha
Serana Zentha - avatar
8 Respostas
25th Jan 2021, 7:22 PM
Avinesh
Avinesh - avatar
+ 1
Avinesh thanks, I'm not good with syntax. I just needed to get rid of two things, its all fixed
25th Jan 2021, 7:26 PM
Serana Zentha
Serana Zentha - avatar
+ 1
Avinesh wait if the first for loop counts the length of the entire array, then why isnt there an error after the third row is surpassed? it just keeps going without the second for loop executing at all, since there is no row beyond the 3rd one until it reaches the requirements
25th Jan 2021, 7:34 PM
Serana Zentha
Serana Zentha - avatar
+ 1
The object 'matrix' is like a container. Consider it has 3 boxes inside. So if I say matrix.length then it will return 3. And each of those boxes contain 3 elements each. 1st box is matrix[0] which has {8,1,6} 2nd box is matrix[1] which has {3,5,7} 3rd box is matrix[2] which has {4,9,0}
25th Jan 2021, 7:46 PM
Avinesh
Avinesh - avatar
+ 1
Avinesh that makes WAY more sense. If I knew that before I would have had lottle trouble. My teacher didnt go over that part of array dimensions in detail so that makes much more sense, since it isnt pin pointing the exact length of each row. Thanks so much
25th Jan 2021, 8:20 PM
Serana Zentha
Serana Zentha - avatar
+ 1
Serana Zentha you're welcome.
25th Jan 2021, 8:22 PM
Avinesh
Avinesh - avatar
0
Update: I removed it because It was not needed to begin with, as I realized, since I'm looking for the length of the row. However, there's a different issue. I'm getting an "Int cannot be dereferenced" for some reason, now I need help with that.
25th Jan 2021, 7:18 PM
Serana Zentha
Serana Zentha - avatar
0
@Avinesh why did you add the -1 at ((arrRow<=matrix.length-1;)) I don't everything except that. Thanks!
27th Aug 2021, 12:59 AM
yugi oh
yugi oh - avatar