Results 1 to 7 of 7
Thread: Accessing objects in array list
- 03-11-2011, 12:26 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 20
- Rep Power
- 0
Accessing objects in array list
I'm building a simple music system which saves album info, currently command line but eventually an application. I create an object 'aCD' which adds all the album info. i then send the object to another class to be stored in an arrayList. Now i want to access that arrayList and use the information stored in the objects so i can say something like (object1.title) and return the title name i had for that object and i've no idea how. I apologise if i've used any incorrect terminology but any help would be much appreciated...
The code in main which creates the object aCD the sends the object to a class to store it.
Java Code:StorageSystem store = new StorageSystem(0); CD aCD; title = "The White Album"; artist = "The Beatles"; playTime = 58; numOfTracks = 12; aCD = new CD(title, artist, playTime, numOfTracks); store.addItem(aCD);
This is the code which adds the object to the array list in the storage system
What i want to do here is use each object that i saved in the arraylist to be used, for instance... object1.playTime ... object2.playTime etcJava Code:ArrayList<Item> itemList = new ArrayList<Item>(); public void addItem(Item hold) { itemList.add(hold); totalStored++; }
Java Code:public void calculatePartyTime() { //code should go here. the first loop was just for me for(int i = 0; i < itemList.size(); i++) { System.out.println("--" + itemList.get(i)); } }
- 03-11-2011, 12:32 AM #2
Member
- Join Date
- Oct 2010
- Posts
- 20
- Rep Power
- 0
Figured it out. :( you spend hours trying to figure something out... write a long forum poost looking for help and then 30 seconds later it comes to you.
for anyone who cares... this is how you access the object method
Arraylisy.get(index).method();
System.out.println(itemList.get(i).playTime());
- 03-11-2011, 12:34 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
arrayList.get(index) returns an object. So you can think of it as an object. Since you can think of it you can do anything you can do with an object with an index in the array. Try using getters to read information from spaces in the arrayList, and setters to write to.
- 03-11-2011, 12:37 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 20
- Rep Power
- 0
- 03-11-2011, 12:38 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Haha, I used to come here asking questions when working on mini projects only to solve it before anyone responds.
Glad you solved it, please mark your thread solved with the thread tools if you are done.
- 03-11-2011, 12:46 AM #6
That is why in many cases people ask for a SSCCE. Often when creating the small example the answer becomes self evident.
- 03-11-2011, 12:49 AM #7
Member
- Join Date
- Oct 2010
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
Vectors - accessing an unknown amount of objects
By counterfox in forum New To JavaReplies: 1Last Post: 05-07-2010, 10:45 PM -
Accessing objects between classes
By bikashg in forum New To JavaReplies: 7Last Post: 05-06-2010, 12:05 PM -
arraylist help please? trying to add objects to an array list
By zhangster in forum New To JavaReplies: 9Last Post: 02-10-2010, 03:19 AM -
Accessing objects from within Action listener
By cog in forum New To JavaReplies: 4Last Post: 12-24-2009, 08:17 PM -
Accessing list out another class
By Preethi in forum New To JavaReplies: 23Last Post: 10-26-2008, 02:54 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks