aspose file tools*
The moose likes Beginning Java and the fly likes Extracting sub-elements from a List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Extracting sub-elements from a List" Watch "Extracting sub-elements from a List" New topic
Author

Extracting sub-elements from a List

Kim Boone
Greenhorn

Joined: Oct 22, 2009
Posts: 3
I am populating a list via an sql query, each element of the list consist of the entire row of data from the db. I want to reference the data by column, however when I use list.get[] I get something like this. [[Ljava.lang.Object;@76c886]. Do I need to use something other then a list?
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Here is how i would do that.

Do not try to expect meaningful text from an object unless you override the toString method.If you don't the it will always display the reference value which is address.


Create a value object for your DB rows.

E.g if the rows retireve about customer data then create a class called Customer with attributes of each column from the table and getter and setter method for them..

Then create new customer objects for each and every row and populate the atribute using setter method or using constructor with arguments and then add the object to the array list.

Then retrieve the data from the list.make sure to cast to Customer type as the return type of the get method is Object if you are not using generics.

From the retrieve Customer object you ca extract the data you want using the getter method.


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

No. The list is fine. It's just that when you say you "get" that data, what you are actually saying is that when you use System.out.println() to write it to the console, that's what you see, right?

If it's an array, then just cast the result of the list's get() method to Object[] -- an array of objects -- and start using the array. What you saw when you printed the array was the output of the array's toString() method, which just prints the type of the array and an internal number. That's irrelevant to how you would actually use the array.
Kim Boone
Greenhorn

Joined: Oct 22, 2009
Posts: 3
Thanks to everyone for your replies.

I ended up using the followng:

Object[] cols = (Object[])results.get(0);
String a = (String)cols[1];
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
The classcast in that example is dangerous; what if it isn't a String? Better to stick with Object and use its toString method, or to have a getXXX method which returns a String.
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
while populating the list you are storing column values in some object and in turn that object in list, is it? Then does that object has getter methods ? If it is not the case, then I don't understand that how in single list element you are populating multiple column of database?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Extracting sub-elements from a List