我有一个狗的ArrayList。如果Fluffy位于ArrayList中,我想将其名称更改为Fido。如果它不在ArrayList中,我想添加它,然后将其名称更改为Fido。
所以我可以检查Fido是否存在于ArrayList中,但是如何检索它以便我可以进行更改?以下是我最接近的。我正在寻找dogs.getElementEqualTo(new Dog("Fido"));
import java.util.ArrayList;
public class Dog {
public static void main(String[] args) {
ArrayList<Dog> dogs = new ArrayList<>();
dogs.add(new Dog("Fido"));
Dog dog = new Dog("Fido");
if (dogs.contains(dog)) {
dog.name = "Fluffy";
}
System.out.println(dogs.get(0).name); //prints Fido
}
String name;
public Dog(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Dog)) return false;
Dog dog = (Dog) o;
return !(name != null ? !name.equals(dog.name) : dog.name != null);
}
@Override
public int hashCode() {
return name != null ? name.hashCode() : 0;
}
}
编辑:
我尝试将我的主要方法改为此,但它不起作用:
ArrayList<Dog> dogs = new ArrayList<>();
dogs.add(new Dog("Fido"));
Dog dog = new Dog("Fido");
if (dogs.contains(dog)) {
dogs.get(dogs.indexOf(dog)).name = "Fluffy";
}
System.out.println(dogs.get(0).name);
答案 0 :(得分:2)
您可以使用以下方式获取Fido的(第一个)索引:
int indexOfFido = dogs.indexOf(new Dog("Fido"));
然后使用:
检索元素dogs.get(indexOfFido);
但是,根据您使用的List
的具体实现,迭代列表可能更有效:
Dog fido = new Dog("Fido");
for (Dog dog : dogs) {
if (fido.equals(dog)) { // Do the comparison this way round, since
// elements of list can be null in general.
// Do whatever.
break; // Unless you want to do the same to all instances of Fido.
}
}
答案 1 :(得分:1)
boolean found = false;
for(Dog a : dogs) {
if(a.name.equals("Fluffy") {
a.name = "Fido";
found = true;
}
if(!found) {
dogs.add(new Dog("Fido"));
}
我不完全确定你的Dog对象在内部是如何工作的,我的回答是基于Dog类是这样的想法:
public static String name;
public Dog(String name) {
this.name = name;
}
答案 2 :(得分:0)
if (dogs.contains(dog))
正在根据需要评估为true,但您正在修改dog
Dog
而不是ArrayList
对象
更改
dog.name = "Fluffy";
要
dogs.get(dogs.indexOf(dog)).name = "Fluffy";
你会得到你想要的结果。
public static void main(String[] args) throws Exception {
ArrayList<Dog> dogs = new ArrayList<>();
dogs.add(new Dog("Fido"));
Dog dog = new Dog("Fido");
if (dogs.contains(dog)) {
dogs.get(dogs.indexOf(dog)).name = "Fluffy";
} else {
dogs.add(dog);
}
System.out.println(dogs.get(0).name); //prints Fido
}
答案 3 :(得分:0)
如果您使用的是Java 8,则可以在filter
流上使用List
,然后依次使用findAny
和最后ifPresent
。
这是一个简单的例子:
public static void main(String[] args) {
List<Thing> things = new ArrayList<>();
things.add(new Thing("thing one"));
things.add(new Thing("thing two"));
System.out.println(things);
things.stream().filter(t -> t.toString()
.contains("one"))
.findFirst()
.ifPresent(t -> t.setName("three"));
System.out.println(things);
}
public static class Thing{
private String name;
public Thing(String s){name=s;}
public String toString(){return "Thing{name="+name+"}";}
public void setName(String s){ name = s;}
public int hashcode(){return name.hashCode();}
public boolean equals(Object o){
return o != null && this.toString().equals(o.toString());
}
}
输出
[Thing{name=thing one}, Thing{name=thing two}] [Thing{name=three}, Thing{name=thing two}]
请注意,第一个元素已被更改。