Results 1 to 7 of 7
  1. #1
    kev670 is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default 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
    Java Code:
       
        ArrayList<Item> itemList = new ArrayList<Item>(); 
    
        public void addItem(Item hold)
        { 
            itemList.add(hold);
    
            
            totalStored++;   
        }
    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 etc
    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));
            }
    
        }

  2. #2
    kev670 is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default

    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());

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  4. #4
    kev670 is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    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.

    Thanks a lot dude

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    That is why in many cases people ask for a SSCCE. Often when creating the small example the answer becomes self evident.

  7. #7
    kev670 is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    That is why in many cases people ask for a SSCCE. Often when creating the small example the answer becomes self evident.
    Now that I've looked up SSCCE :) I totally agree. getting rid of the rubbish helped

Similar Threads

  1. Vectors - accessing an unknown amount of objects
    By counterfox in forum New To Java
    Replies: 1
    Last Post: 05-07-2010, 11:45 PM
  2. Accessing objects between classes
    By bikashg in forum New To Java
    Replies: 7
    Last Post: 05-06-2010, 01:05 PM
  3. Replies: 9
    Last Post: 02-10-2010, 04:19 AM
  4. Accessing objects from within Action listener
    By cog in forum New To Java
    Replies: 4
    Last Post: 12-24-2009, 09:17 PM
  5. Accessing list out another class
    By Preethi in forum New To Java
    Replies: 23
    Last Post: 10-26-2008, 03:54 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
  •