Java,通过arraylist调用对象方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18435992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:36:12  来源:igfitidea点击:

Java, call object methods through arraylist

javaoopobjectarraylist

提问by H J

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”,它包含各种“person”对象。我想基于 for 循环自动创建“人物”对象。我做了以下

    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.

我想从 person 类调用方法。例如 person.setAge; 如何通过数组列表调用这些方法?我想要为每个对象设置值的方法。我看过这个答案:Java - 调用 ArrayList 中对象的方法
但我认为解决方案取决于调用静态方法,我希望在存储对象值时使用特定于对象的方法。

采纳答案by Pshemo

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

或使用增强的 for 循环

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


BTW you should stick with Java Naming Conventionsand use camelCase style. Classes/interfaces/enums should starts with upper-case letter like Person, first token of variables/methods name should start with lower-case but others with upper-case like peopleHolder.

顺便说一句,您应该坚持使用Java 命名约定并使用驼峰式风格。类/接口/枚举应以大写字母开头,如Person,变量/方法名称的第一个标记应以小写开头,但其他标记应以大写开头,如peopleHolder

回答by Gaurav Varma

Is this what you are looking for ?

这是你想要的 ?

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

回答by nachokk

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

我有几个人对象。我使用循环在“people” arrayList 中自动创建新的 person 对象。我想访问定义对象的各种特征的方法(setAge、setHeight 等)如何访问我在数组列表中创建的对象的这些方法

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).

对于列表中的某些索引,您可以使用,get(int index)因为您的列表是一个 arrayList,那么它是 O(1)。

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

回答by William Moffitt

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);

}