This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

method calling in ArrayLists

 
Jean Perry
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jinu Vijay
Greenhorn
Posts: 20
Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49405
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1376
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jean Perry
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic