Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes method calling in ArrayLists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "method calling in ArrayLists" Watch "method calling in ArrayLists" New topic
Author

method calling in ArrayLists

Jean Perry
Greenhorn

Joined: Mar 12, 2013
Posts: 6
say I have an 2 array lists
1. an ArrayList<People>
2. an ArrayList<PhoneInfomation>

the people object has an reference to the array list of phoneInfomation. what is the correct syntax to call upon a method in the phoneInformation to retrieve like a "phoneNumber" data member?

I was doing something like:



//get(int) method takes an index and return the object
//getPhoneNumber() takes nothing and return a int (phoneNumber); found in the people class

does that make sense? or does this only make sense in my reality?
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Do me a favor Jean. Forget the code for now. Just write a summary of what your use-case is. What is it that you are trying to achieve? Is it purely storing information about people (names/addresses/phone numbers etc..) or is it something more than that? Then I would be able to provide you with some guidance on how to go about doing that.


~ Mansukh
Jinu Vijay
Greenhorn

Joined: Sep 19, 2011
Posts: 20

Jean Perry wrote:say I have an 2 array lists
1. an ArrayList<People>
2. an ArrayList<PhoneInfomation>

the people object has an reference to the array list of phoneInfomation. what is the correct syntax to call upon a method in the phoneInformation to retrieve like a "phoneNumber" data member?



For starters am assuming your code snippet is just an algo (not syntactically written! ). I'd say your sample seems a bit flawed w.r.t your requirement.

everyone.get(i) would give you an object of People. You still got to get the ArrayList of Phoneinfo from it, and then pick your required object!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Jean Perry wrote: . . .

does that make sense? or does this only make sense in my reality?
Yes, it does make sense. It depends on what you have in that List, and surely the People class would be better named Person.
It is very strange that a Person would have a List of phone numbers, however, unless they are his own phone numbers. I think you have some iffy design there, which ought to be chnaged.
There is another problem in your loop. You are obtaining something and doing nothing with it, so the reference to whatever you get simply disappears into cyber‑limbo never to be seen again.
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1189
    
    1

for example - PhoneInformation class have a method getPhoneCode()


Now you are an arrayList of Person say - PersonArrayList.

So first perform a get operation on PersonArrayList baed on any index --

This get operation will return you a Person object.
Now in that object, you have Phone Info Array List. So , you need to call a method of Person class to get this phoneArrayList.


Now you have PhoneArrayList with you.
Assuming you have mobile number of that person at index 0, landline number of person ar index 1.
now you want to fetch mobils number of that person.
So need to perform get operation on PhoneArrayList at index 1 to get mobile number.




~abhay

Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I, CIW Certified Ecommerce specialist
Jean Perry
Greenhorn

Joined: Mar 12, 2013
Posts: 6
Abhay Agarwal wrote:for example - PhoneInformation class have a method getPhoneCode()


Now you are an arrayList of Person say - PersonArrayList.

So first perform a get operation on PersonArrayList baed on any index --

This get operation will return you a Person object.
Now in that object, you have Phone Info Array List. So , you need to call a method of Person class to get this phoneArrayList.


Now you have PhoneArrayList with you.
Assuming you have mobile number of that person at index 0, landline number of person ar index 1.
now you want to fetch mobils number of that person.
So need to perform get operation on PhoneArrayList at index 1 to get mobile number.




~abhay


thank you, that makes a lot of sense
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: method calling in ArrayLists