This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes External method calls in an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "External method calls in an ArrayList" Watch "External method calls in an ArrayList" New topic
Author

External method calls in an ArrayList

John Molloy
Greenhorn

Joined: Apr 26, 2009
Posts: 7
Hey,

I'm doing my work for uni, which is just an ongoing project which we add to each week. This week we have to change one of the arrays to an ArrayList instead. What I'm having trouble with is calling methods with the ArrayList Object. Anyway I'll post the code which may explain it a little better. Line 69 on is whats giving me trouble.



So what I'm trying to do is call the getEmployees() and getSize() from the Office class. Now i could do this in the array buy just specifying an element in the array factoryOffice[0].getEmployees() would work, but not with the ArrayList.
Here's the methods in the office class. I get a method not found error.



Cheers
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 20137
    
  30

Check out java.util.List and java.util.Iterator.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
John Molloy
Greenhorn

Joined: Apr 26, 2009
Posts: 7
I have looked at both of those but I still cant find the answer. Pretty much I'm just wanting to know is if an array worked like this:

factoryOffices[0].getEmployees()

How would I do that in an arraylist and yes it does have to be an arraylist.

Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 20137
    
  30

You may have looked at those two interfaces, but you certainly have not read the pages.

ArrayList implements List, so just go through all of its methods. You're certain to find one you want. Trust me.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44616
    
  34
An ArrayList<E> is a List<E>, so whatever you can do in the List interface you can do in the ArrayList class. Look up what Rob showed you, particularly methods of the List interface which might allow you to get something.

And have a look at this Java™ Tutorials section.
John Molloy
Greenhorn

Joined: Apr 26, 2009
Posts: 7
Well after reading over the page 5 times (3 am here) I finally figured out what you guys were meaning.

factoryOffices.get(0).getEmployees() worked.

Thanks a bunch.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44616
    
  34
Well done
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: External method calls in an ArrayList
 
It's not a secret anymore!