+ Reply to Thread
Results 1 to 8 of 8

Thread: Java ArrayLists

  1. #1
    Member theGlitch's Avatar
    Join Date
    Jul 2008
    Location
    Boston, MA
    Posts
    185
    Blog Entries
    1

    Java ArrayLists

     
    Hey all. I'm working on an application that will take an ArrayList filled with objects and parse the objects into an xml document. My issue is, the objects I'm trying to access are losing their type by being added to my ArrayList.

    In other words I'm adding a Change object to my ArrayList. When I try to access it later it reads it as a plain Object.

    Code:
    currentSVR = Change.getServer(stripped.get(0));
    throws:
    getServer(myproject.Change) in myproject.Change cannot be applied to (java.lang.Object)
    Everything is in the same package and works otherwise.

  2. #2
    Member
    Join Date
    Oct 2003
    Posts
    267
    im a .net guy but i believe array lists in java are exactly the same.
    An array list contains a list of objects. So when you grab the item from the arraylist you will need to cast it back into whatever type it is. Otherwise it will come back as an object.

    You would want to stay away from bringing everything back as objects cause it will be late bound (in .net atleast I assume its the same as Java)

    What does the getServer function take as a parameter? As specific object type or just an object?

  3. #3
    Member theGlitch's Avatar
    Join Date
    Jul 2008
    Location
    Boston, MA
    Posts
    185
    Blog Entries
    1
    Code:
    public static String getServer (Change c){
                      String server = c.s;
                      return server;
              }
    so if I understand you correctly, the following should work

    Code:
    currentSVR = Change.getServer((Change)stripped.get(0));

  4. #4
    Member
    Join Date
    Oct 2003
    Posts
    267
    the only time it wouldn't work is if the arraylist contains an object that is not a "Change".

    Instead of using an arraylist use a linked list or an array of "change". This will verify that everything in the list will be of type "change".

  5. #5
    Member theGlitch's Avatar
    Join Date
    Jul 2008
    Location
    Boston, MA
    Posts
    185
    Blog Entries
    1
    the problem with using a regular array is that it needs to be dynamic. I won't know how many Change objects I'll need.

    EDIT:
    Linked Lists don't seem to hold the type either.
    Last edited by theGlitch; December 9th, 2009 at 03:42 PM.

  6. #6
    Junior Member
    Join Date
    Oct 2008
    Posts
    25
    Use Generics.

    For example:

    Code:
    ArrayList<Change> a = new ArrayList<Change>();
    This will allow for the Change objects added to the ArrayList to retain their type, so you don't have to do any casting.

    You can also use generics for LinkedLists.
    Last edited by Is_It_Just_Me?; December 10th, 2009 at 07:10 AM.

  7. #7
    Junior Member
    Join Date
    Dec 2009
    Posts
    4
    thanks for your information on arrays

  8. #8
    Member theGlitch's Avatar
    Join Date
    Jul 2008
    Location
    Boston, MA
    Posts
    185
    Blog Entries
    1
    It's been so long since I last used Generics, I had completely forgotten about them... Thanks!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Launching Java webpages with different Java settings?
    By LostBok in forum Webmastering and Programming
    Replies: 1
    Last Post: December 11th, 2006, 03:38 AM
  2. Java HELP.
    By carl33p in forum Webmastering and Programming
    Replies: 3
    Last Post: November 14th, 2006, 04:50 PM
  3. java help
    By sunny22 in forum Webmastering and Programming
    Replies: 6
    Last Post: January 13th, 2006, 04:13 PM
  4. JAVA help please?
    By Martoch in forum Webmastering and Programming
    Replies: 5
    Last Post: February 4th, 2003, 02:38 AM

Tags for this Thread

Posting Permissions

  • You may post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Recommended Sites: ResellerRatings Store Reviews