• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

method calling in ArrayLists

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 20
Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic