Thread: Java ArrayLists
-
December 3rd, 2009, 05:47 PM #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.
throws:Code:currentSVR = Change.getServer(stripped.get(0));
Everything is in the same package and works otherwise.getServer(myproject.Change) in myproject.Change cannot be applied to (java.lang.Object)
-
December 3rd, 2009, 11:14 PM #2Member
- 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?
-
December 4th, 2009, 09:41 AM #3so if I understand you correctly, the following should workCode:
public static String getServer (Change c){ String server = c.s; return server; }
Code:currentSVR = Change.getServer((Change)stripped.get(0));
-
December 4th, 2009, 09:56 AM #4Member
- 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".
-
December 9th, 2009, 01:30 PM #5
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.
-
December 10th, 2009, 07:05 AM #6Junior Member
- Join Date
- Oct 2008
- Posts
- 25
Use Generics.
For example:
This will allow for the Change objects added to the ArrayList to retain their type, so you don't have to do any casting.Code:ArrayList<Change> a = new ArrayList<Change>();
You can also use generics for LinkedLists.
Last edited by Is_It_Just_Me?; December 10th, 2009 at 07:10 AM.
-
December 10th, 2009, 07:49 AM #7Junior Member
- Join Date
- Dec 2009
- Posts
- 4
thanks for your information on arrays
-
December 10th, 2009, 11:51 AM #8
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
-
Launching Java webpages with different Java settings?
By LostBok in forum Webmastering and ProgrammingReplies: 1Last Post: December 11th, 2006, 03:38 AM -
Java HELP.
By carl33p in forum Webmastering and ProgrammingReplies: 3Last Post: November 14th, 2006, 04:50 PM -
java help
By sunny22 in forum Webmastering and ProgrammingReplies: 6Last Post: January 13th, 2006, 04:13 PM -
JAVA help please?
By Martoch in forum Webmastering and ProgrammingReplies: 5Last Post: February 4th, 2003, 02:38 AM



LinkBack URL
About LinkBacks




Reply With Quote

Ugh, and I just got done doing an uninstall of one of Windows Botched updates from months ago, that seems to have affected many users for various programs, in my case, FRAPS doesn't record the...
nVidia GeForce 320.18 WHQL Driver...