• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multi Dimensional Array Error

 
Saad Zahoor
Ranch Hand
Posts: 70
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question : it give exception error . i want to create 3 rows and 2 elements in each row !! Help


 
Dave Tolls
Ranch Hand
Posts: 2369
25
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the full exception and stack trace that you are getting?
It contains information that should tell you what the problem is (though I think I can spot it).
 
Henry Wong
author
Sheriff
Posts: 22616
111
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to keep track of the array lengths. The myArray.length is the number of int arrays -- and should be used by the outer loop only. The number of int elements in those int arrays are determined by myArray[0].length, myArray[1].length, and myArray[2].length respectively. The inner loop probably want to use myArray[i].length to get those values.

[EDIT] Oops, sorry for just kinda dropping the answer, when Dave was already working on helping the OP work towards it. I didn't see Dave's post. Anyway, have a cow for the inconvenience.

Henry
 
Tim Cooke
Sheriff
Posts: 3457
163
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Plus I added Code Tags to your post. It makes the code much easier to read don't you agree? Read up on how to use them here -> Wiki: Use Code Tags

As Dave suggests, the stack trace for the Exception will give you a good idea where you have gone wrong here. I too have spotted the error but it'll be a good exercise for you to use the error message to debug your own program.

So, let's see it.
 
Liutauras Vilda
Marshal
Posts: 3401
131
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is with an inner loop. Can you think for a moment and identify what is wrong?
Hint: your outer array contains 3 elements in it which are arrays themselves, so you iterate over them with an outer for loop, and then having an inner loop to iterate over each of those 3 elements (which are arrays) whose length are 2. But your inner loop trying to iterate over how many elements?
 
Saad Zahoor
Ranch Hand
Posts: 70
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now i understand i should type the code like this

for (int j = 0; j < myArray[i].length; j++)

but anyone can explain this what is happening !!

at myArray [i].length ;
 
Henry Wong
author
Sheriff
Posts: 22616
111
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saad Zahoor wrote:
but anyone can explain this what is happening !!  at myArray [i].length ;


If you don't think of it as a multi-dimensional array, but as the more correct concept of "array of arrays" (which is what Java supports). Does that help?

Henry
 
Liutauras Vilda
Marshal
Posts: 3401
131
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Outer array
A.length = 3

Inner array
A[0].length = 2

When you iterate over an inner array by supplying outer array's length, you're trying to access the element at position A[2] which is third element and which does not exist, as you have only 2 elements in it. So you need explicitly specify that you want to iterate over the length of an array A[0] (at this element is an inner array of length 2), rather than over length of array A (which length is 3).

Not sure if that is clear - probably not, I find it difficult to explain and I know how difficult could be to understand at the beginning. But no worries, it will make sense after a while.
 
Liutauras Vilda
Marshal
Posts: 3401
131
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this post (and whole thread), maybe there you'll find more useful information.
 
Saad Zahoor
Ranch Hand
Posts: 70
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NOW I Understand ... thanks guys ,, just want to ask one more Question .. there are two loops .. one will execute and it will go the second loop like in array case and it check the second loop n then go to the first one ...
i mean will it display all the content of second n then it will go ?
 
Liutauras Vilda
Marshal
Posts: 3401
131
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saad Zahoor wrote:will it display all the content of second n then it will go ?

What it displayed when you tried?
 
Saad Zahoor
Ranch Hand
Posts: 70
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yea it is .. but i am understanding the concept ... well thanks .. now my problem is fixed
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!