简体   繁体   English

Java,通过arraylist调用对象方法

[英]Java, call object methods through arraylist

Based on this question Increment variable names? 基于这个问题增量变量名称?

I have an arraylist 'peopleHolder' which holds various 'person' objects. 我有一个arraylist'peopleHolder',它拥有各种“人物”物品。 I would like to automatically create 'person' objects based on a for loop. 我想基于for循环自动创建'person'对象。 I did the following 我做了以下

    peopleHolder.add(new person());

I would like to call methods from the person class. 我想从人类中调用方法。 for example person.setAge; 例如person.setAge; How can I call such methods through an arraylist? 我如何通过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 我看了这个答案: Java - 调用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. 但我认为解决方案依赖于调用静态方法,并且我希望在存储对象值时让对象特定于该对象。

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 Conventions and use camelCase style. 顺便说一下,你应该坚持使用Java命名约定并使用camelCase风格。 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 . 类/接口/枚举应以大写字母开头,如Person ,变量/方法名称的第一个标记应以小写字母开头,而其他大写字母peopleHolder

Is this what you are looking for ? 这是你想要的 ?

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

I have several person objects. 我有几个人物。 I automatically create new person objects in a 'people' arrayList using a loop. 我使用循环在'people'arrayList中自动创建新的person对象。 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 我想访问定义对象的各种特征的方法(setAge,setHeight等)。如何访问我在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). 对于列表中的某个索引,可以使用get(int index)因为列表是一个arrayList,然后是O(1)。

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

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM