Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

External method calls in an ArrayList

 
John Molloy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out java.util.List and java.util.Iterator.
 
John Molloy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50217
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50217
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic