Results 1 to 5 of 5
  1. #1
    keo
    keo is offline Member
    Join Date
    May 2010
    Posts
    34
    Rep Power
    0

    Default ArrayList problem.

    I have a Box class, Bag class and a Apple class .
    the Bag class contains an ArrayList.

    BAG CLASS
    Java Code:
    ArrayList<Apple> apples = new ArrayList<Apple>();
    
    public int getSize() {
    int i = apples.getSize();
    return i;
    }
    BOX CLASS
    Java Code:
    Bag.apples.getSize();
    I want to getSize() the number of apples by asking the bag class.

    How do i bring the array over to box class to check the getSize() method? Because I know I can't use 2 calls.
    If I Bag.getSize, its asking the whole class (non static...) but I also don't want to create a new array to get back 0.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    use an instance of the Bag class rather than "Bag" in that call and remove ".apples" from that call.

  3. #3
    keo
    keo is offline Member
    Join Date
    May 2010
    Posts
    34
    Rep Power
    0

    Default

    Java Code:
    Bag b;
    
    b.getSize();
    So if I do that, b can identify the Arraylist automatically?
    Thanks, Its complied.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    Bag has a "getSize()" method (according to that code), and that method calls "size()" on the instance variable, so, yeah. I mean, that's what all that is for, after all.

  5. #5
    keo
    keo is offline Member
    Join Date
    May 2010
    Posts
    34
    Rep Power
    0

    Default

    Oh! Thanks! I've been coding all day I don't know whats going on anymore. haha.

Similar Threads

  1. Arraylist problem
    By keo in forum New To Java
    Replies: 3
    Last Post: 05-28-2010, 10:38 AM
  2. ArrayList problem
    By brian.baert in forum New To Java
    Replies: 8
    Last Post: 03-06-2010, 02:49 PM
  3. Hey! ArrayList problem here
    By Samgetsmoney in forum New To Java
    Replies: 31
    Last Post: 02-20-2009, 01:39 AM
  4. ArrayList problem with images
    By Cymro in forum New To Java
    Replies: 2
    Last Post: 02-05-2008, 07:22 PM
  5. ArrayList problem
    By khamuruddeen in forum New To Java
    Replies: 7
    Last Post: 12-22-2007, 06:46 AM

Posting Permissions

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