Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extracting sub-elements from a List

 
Kim Boone
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Pie
Posts: 20958
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic