java 替换 ArrayList 中的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13728239/
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
Replacing an object in a ArrayList
提问by user1315906
I need to know how to replace an object which is in a ArrayList<Animal>
.
我需要知道如何替换ArrayList<Animal>
.
I have a array list called ArrayList<Animal>
. This list only has 5 animals listed init.
我有一个名为ArrayList<Animal>
. 此列表仅列出了 5 只动物。
Animal animal = new Animal();
animal.setName("Lion");
animal.setId(1);
ArrayList<Animal> a = new ArrayList<Animal>();
a.put(animal); // likewise i will be adding 5-6 animals here.
Later on, i will take an object from this array list and modify it.
稍后,我将从这个数组列表中取出一个对象并修改它。
Animal modifiedAnimal = new Animal();
animal.setName("Brown Lion");
animal.setId(1);
Now i need to add this Animal Object to the ArrayList<Animal> a
array list. I need it to replace the previous Animal
object which we named it Lion
. How can i do this ?
现在我需要将此动物对象添加到ArrayList<Animal> a
数组列表中。我需要它来替换Animal
我们命名它的前一个对象Lion
。我怎样才能做到这一点 ?
Note: I don't know the Index where this object is added.
注意:我不知道添加此对象的索引。
采纳答案by durron597
Look at bellum's post for how to do this with an ArrayList
. However, you'd probably be better off with a HashMap<String, Animal>
查看 bellum 的帖子,了解如何使用ArrayList
. 但是,你可能会更好HashMap<String, Animal>
HashMap<String, Animal> animals = new HashMap<String, Animal>();
// ...
Animal animal = new Animal();
animal.setName("Lion");
animal.setId(1);
animals.put(animal.getName(), animal);
// to modify...
Animal lion = animals.remove("Lion"); // no looping, it finds it in constant time
lion.setName("Brown Lion");
animals.put(animal.getName(), animal);
回答by T.J. Crowder
If you really want to replacethe Animal
in the list with a whole new Animal
, use List#set
. To use List#set
, you'll need to know the index of the Animal
you want to replace. You can find that with List#indexOf
.
如果你真的想取代了Animal
一个全新的列表Animal
,使用List#set
。要使用List#set
,您需要知道Animal
要替换的索引。你可以用List#indexOf
.
If you just want to modifyan animal that is already in the set, you don't have to do anything with the list, since what's stored in the list is a reference to the object, not a copy of the object.
如果你只想修改一个已经在集合中的动物,你不必对列表做任何事情,因为列表中存储的是对对象的引用,而不是对象的副本。
回答by bellum
You must first get its reference from list and simply modify it. Like this:
您必须首先从列表中获取它的引用并简单地修改它。像这样:
Integer yourId = 42;
for (Animal a : animalList){
if (a.getId().equals(yourId)){
a.setName("Brown Lion");
//...
break;
}
}
回答by PermGenError
for(Animal al: a){
if(al.getName().equals("Lion")){
al.setName("Brown Lion");
al.setId(1);
break;
}
}
- Loop thru your list untill you find the Animal
- change its attributes
- break outta loop
- 循环遍历您的列表,直到找到动物
- 改变它的属性
- 跳出循环
回答by someone
You could use HashMap for this easily. Your key is id and value is animal object.
您可以轻松地为此使用 HashMap。您的键是 id,值是动物对象。
by using get
method you can take the animal object you required and do the modification and again put new Animal against same key.
通过使用get
方法,您可以获取所需的动物对象并进行修改,然后再次将新动物放在同一键上。