• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Multi Dimensional Array Error

 
Ranch Hand
Posts: 92
Android
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question : it give exception error . i want to create 3 rows and 2 elements in each row !! Help


 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 5950
407
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9012
655
Mac OS X Spring VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 92
Android
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 9012
655
Mac OS X Spring VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 9012
655
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this post (and whole thread), maybe there you'll find more useful information.
 
Saad Zahoor
Ranch Hand
Posts: 92
Android
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 9012
655
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 92
Android
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yea it is .. but i am understanding the concept ... well thanks .. now my problem is fixed
 
reply
    Bookmark Topic Watch Topic
  • New Topic