File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Parsing values to a 2D array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Parsing values to a 2D array" Watch "Parsing values to a 2D array" New topic

Parsing values to a 2D array

Sean Michael Hayes
Ranch Hand

Joined: Feb 08, 2012
Posts: 54

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

Joined: Dec 08, 2010
Posts: 1545

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.

Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
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

Joined: Oct 13, 2005
Posts: 46412
Why are you not creating a class to encapsulate your data, instead of arrays?
Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

And use a List to store that data.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Parsing values to a 2D array
It's not a secret anymore!