Results 1 to 5 of 5
Thread: List<Serializable> help
- 03-18-2011, 02:11 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 23
- Rep Power
- 0
List<Serializable> help
Hello,
I am new to Java and I have inherited an existing program. I am in the data layer and have returned a List<Serializable>. When I add a watch to list I can see the data I am looking for however I can’t figure out how to access the data programmatically. I tried something like:
List<Serializable> myList = result.getAllStaff();
for(Serializable key : myList)
{
Key.???? Nothing here to access data
{
Is there a way to access the data that I can see when I put a watch on the myList?
Thanks
- 03-18-2011, 02:15 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
What do you mean by watch? Your list contains a bunch of objects defined by the interface Serializable, which is a marker interface with no methods. If you want to access other things from the object, you can either a) return a List containing more meaning information or b) cast the objects. For instance, if you guess the classes are an instance of "MyClass"
Java Code:for ( Serializable key : myList ){ if ( key instanceof MyClass ){ MyClass cl = (MyClass)key; //you now have access to the methods of MyClass } }
- 03-18-2011, 02:21 PM #3
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
How does the method getAllStaff() return data back? Is it in the same form: List<Serializable>?
I would advise you to do make a list that can only accept Staff objects: List<Staff>
Then your getAllStaff method return value would be the same as in myList.
Now if your Staff object needs methods to be able to view certain data.
In the for loop you could say:
ORJava Code:for(Staff member : myList) { member.getName(); //<--- method must be in the object {
From the code you have no, the only thing that might work is do:
Key.toString();
It would return just location of where the data is stored in memory. At least you'll know that data exists.Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
- 03-18-2011, 03:26 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 23
- Rep Power
- 0
Thanks,
By watch I mean when I run the code I can put a watch on the key and see the contents.
I am not sure what the AllStaff returns, I think it is XML, but I am new to this and not 100% sure. When I watch the results I see several elements under the list, the particular one I am trying to get in this case is called value.
It is laid out like this:
I get the list
List<Serializable> myList = result.getAllStaff();
In the list I have the following elements:
- “myList”=ArrayList
-elementData=Object[10]
-[0]=JAXBElement<T>
+declaredType=Class<T>Infos
+name=QName
Nil=false
+value=Ticketing
+[1] JAXBElement<T>
….
When I expand the value in the results above I can see the data that I want. That class is of type Ticketing. If I can get to the value node I could then cast it as Ticketing but I cant figure out how to get to the Value node.
Thanks again.
- 03-18-2011, 04:28 PM #5
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
Ok I get it.
Well in that case try to put the values inside an array:
It might not be right, but the idea could be worth looking into.Java Code:ArrayList<String> staff = new ArrayList<String>(); staff = myList.toArray(); for(String s:staff){ System.out.println(s); }
Try to look through List Documentation and see if you spot anything that might work for you.Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
Similar Threads
-
Make a thread Serializable
By madnaelo in forum Advanced JavaReplies: 2Last Post: 01-18-2011, 04:37 AM -
Serializable problem
By javaPower in forum Advanced JavaReplies: 3Last Post: 11-30-2010, 10:54 AM -
marking a field Serializable
By user1234 in forum Advanced JavaReplies: 9Last Post: 05-18-2010, 11:27 AM -
Casting to an List<Serializable>
By Whatty in forum Advanced JavaReplies: 5Last Post: 10-15-2009, 07:47 PM -
Implementing Serializable interface
By javaplus in forum Advanced JavaReplies: 4Last Post: 12-18-2007, 12:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks