jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Placing ArrayList into Object Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Placing ArrayList into Object Array" Watch "Placing ArrayList into Object Array" New topic

Placing ArrayList into Object Array

Jas Oberai
Ranch Hand

Joined: Jan 16, 2005
Posts: 231
Hi Friends,
I have a real simple question.I have a arraylist and I just want to put the arraylist contents into an object array for some reason and print back the same results.I dont know why the object array it's showing me more results then I actually need.Here's my code,please please help me:
I get the arraylist populated from a database table which has 8 columns.

SCJP 1.4 (88%)<br />SCWCD 1.4 (88%)
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
First of all, it would be a LOT easier to read your code if your indentation were consistent. Something like the following is much easier to read, imo:

Now as I look through this code, the first thing I notice that seems somewhat strange to me, is the initialization code:

Why do you increment nrows with "nrows=nrows+1"? This doesn't seem necessary to me, and is probably contributing to the extra results you see in your output. Although, on second thought, this line of code may be necessary if the size of the ArrayList is not a multiple of 8. However, if that is the case, then your approach here is somewhat supsicious. To me, it wouldn't make sense to create a 2D array from the ArrayList unless you can guarantee that its size is a multiple of 8 (or whatever you decide is the number of elements in each row).

However, if remove that one line of code, the rest still seems overly complicated, imo, for such a straight-forward task. The next thing that looks suspicious to me is the following for loop:

Typically, if you want to indicate that an element in an array is empty you set it to a null value, rather than using a String with the value "null". What I mean is something like this:

Even this is unneccessary, though, since the value of each element in the array is automatically set to null when you create it with

This means the above for loop is completely unnecessary.

The next thing that sticks out to me, is the following line of code:

The array has already been completely initialized, so this is redundant. Also, it throws out the results of the for loop discussed above anyways (yet another reason the for loop is not needed).

The for loop that contains the above line of code could be simplified more than just removing that line. At the moment, this for loop looks like this:

This could be simplified to the following:

Notice that this uses integer division to calculate the row number and modulus to calculate the column number.

Even with these changes I'm not totally confident that it will fix your problem. However, I hope it simplifies your code a little so it's easier to track down what is going on.

Alternatively, the Java API provides ArrayList.toArray() to convert from a List to an Object[]. Notice that this will only give you a 1D array, so if you really need a 2D array, you would still have to do some work. Also, if you use the toArray() method that takes no parameters, the Objects in the returned array cannot be typecast to a subclass. If you need to do this, you need to use ArrayList.toArray(Object[]). I will refer you to the API documentation for ArrayList if you are interested in more details.



Java API Documentation
The Java Tutorial
Jas Oberai
Ranch Hand

Joined: Jan 16, 2005
Posts: 231
Thanks a lot layne,
Tht simplied my code and understanding a lot.Actually I am doing a project and this was just a part of it,and I still have to resolve this problem by myself.I was so tired with this code,that I forgot the indentation part.Anywayz thanks a lot buddy.
I agree. Here's the link: http://aspose.com/file-tools
subject: Placing ArrayList into Object Array
It's not a secret anymore!