This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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.
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.