6

Based on this question Increment variable names?

I have an arraylist 'peopleHolder' which holds various 'person' objects. I would like to automatically create 'person' objects based on a for loop. I did the following

    peopleHolder.add(new person());

I would like to call methods from the person class. for example person.setAge; How can I call such methods through an arraylist? I would like the method to set values for each object. I have looked at this answer: Java - calling methods of an object that is in ArrayList
But I think the solution depends on calling static method and I would like to have the method specific to the object as they store the objects value.

4
  • your question is not clear, what are you meaning? Commented Aug 26, 2013 at 3:07
  • I have several person objects. I automatically create new person objects in a 'people' arrayList using a loop. I would like to access methods that define various characteristics of the objects( setAge, setHeight etc.) How can I access such methods for the objects I have created in my arraylist. Commented Aug 26, 2013 at 3:09
  • you want to create Arraylist reference into person class, am I right? Commented Aug 26, 2013 at 3:09
  • ArrayList<Person> peopleHolder = new ArrayList<Person>();peopleHolder.add(new Person());for(Person p:peopleHolder){p.setAge();} Commented Aug 26, 2013 at 3:56

4 Answers 4

13

If you want to call some method at all objects from your list you need to iterate over them first and invoke method in each element. Lets say your list look like this

List<person> peopleHolder = new ArrayList<person>();
peopleHolder.add(new person());
peopleHolder.add(new person());

Now we have two persons in list and we want to set their names. We can do it like this

for (int i=0; i<list.size(); i++){
    list.get(i).setName("newName"+i);//this will set names in format newNameX
}

or using enhanced for loop

int i=0;
for (person p: peopleHolder){
    p.setName("newName" + i++);
}

BTW you should stick with Java Naming Conventions.

  • your types, so classes and interfaces (which includes enums, records, etc.) should starts with upper-case like class Person {..}, not class person {..}
  • your variables and methods should start with lower-case like peopleHolder.
Sign up to request clarification or add additional context in comments.

3 Comments

Pretty sure that should be list.get(i) instead of list.get(0).
Thanks Pshemo. In the setName method why do you add the i?
@HJ I used i to set different names, like newName0, newName1, and so on :)
3

Is this what you are looking for ?

for(person people: peopleHolder){
people.setAge(25);
}

Comments

2

I have several person objects. I automatically create new person objects in a 'people' arrayList using a loop. I would like to access methods that define various characteristics of the objects( setAge, setHeight etc.) How can I access such methods for the objects I have created in my arraylist

In the same way as you create.

For every person in list you can iterate it

for(Person person : peopleHolder){
  person.setHeight(..);
  person.setAge(..)
}

For some index in the list, you can use get(int index) as your list is an arrayList then it's O(1).

 Person p = peopleHolder.get(someIndex); // where  0 <= someIndex < peopleHolder.size()
 p.setHeight(..);

Comments

1

Following your code you could do this

ArrayList<Person> personHolder = new ArrayList<Person>();

for (int i = 0; i < 10; i++) {

    personHolder.add(new Person());

}

// If you want user input, do it in the loop below
for (Person p : personHolder) {

    p.setAge(30);

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.