• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parsing values to a 2D array

 
Sean Michael Hayes
Ranch Hand
Posts: 54
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an object that I want to create from an xml document. The problem is that there are certain values in the xml document I want to put in a 2D array. I.E



The 2D array shall a fixed amount of columns but N amount of rows depending on the device. How would you go about parsing this? My current set-up gets me an ArrayIndexOutOfBoundsException



the Set row method is meant to change the value of the snmpObject's 2D array. Any pointers would be helpful.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi SeanMichael Hayes,

Welcome to CodeRanch!

Good to see that you are using code tags.

Now, coming to your problem, an ArrayIndexOutOfBoundsException is natural because of line no. 3 and 4 of your Java code.

Please note that in Java, there are no n-dimensional (2D,3D etc.) arrays. What you are declaring is an array of arrays (and not a 2D array). In your code, you are initializing count as 0 and then passing it as size of array. So, in turn, you are creating an array of size 0. Obviously, the moment you access any element of that array, its gonna throw that typical exception.

If value of count is not known beforehand, and you are only concerned about key-value pairs (i.e. number of 'columns' is always 2), then how about using a Map (say HashMap)? That way, you don't need to declare its size during initialization.

I hope this helps.
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you write



how can you expect to have multiple rows? You defined a zero-rows array.
You will always get an ArrayIndexOutOfBoundsException there, when you try to populate it.

 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you not creating a class to encapsulate your data, instead of arrays?
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And use a List to store that data.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic