Results 1 to 5 of 5
  1. #1
    BobswYourUcle is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default 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

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    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
        }
    }

  3. #3
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    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:

    Java Code:
    for(Staff member : myList)
    {
     member.getName(); //<--- method must be in the object
    {
    OR
    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.

  4. #4
    BobswYourUcle is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    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.

  5. #5
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Ok I get it.

    Well in that case try to put the values inside an array:

    Java Code:
    ArrayList<String> staff = new ArrayList<String>();
    
    staff = myList.toArray();
    
    for(String s:staff){
    System.out.println(s);
    }
    It might not be right, but the idea could be worth looking into.

    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

  1. Make a thread Serializable
    By madnaelo in forum Advanced Java
    Replies: 2
    Last Post: 01-18-2011, 04:37 AM
  2. Serializable problem
    By javaPower in forum Advanced Java
    Replies: 3
    Last Post: 11-30-2010, 10:54 AM
  3. marking a field Serializable
    By user1234 in forum Advanced Java
    Replies: 9
    Last Post: 05-18-2010, 11:27 AM
  4. Casting to an List<Serializable>
    By Whatty in forum Advanced Java
    Replies: 5
    Last Post: 10-15-2009, 07:47 PM
  5. Implementing Serializable interface
    By javaplus in forum Advanced Java
    Replies: 4
    Last Post: 12-18-2007, 12:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •