Java 检测并防止名称重复ArrayList
我的问题正如标题所说:我想检测并防止ArrayList中的重复名称。我不能使用任何集合方法 这是我的密码:Java 检测并防止名称重复ArrayList,java,arraylist,Java,Arraylist,我的问题正如标题所说:我想检测并防止ArrayList中的重复名称。我不能使用任何集合方法 这是我的密码: private static void kommandoEtt() { Kund nyKund = new Kund(); System.out.print("Name: "); nyKund.setNamn(tangentbord.nextLine()); kundregister.add(nyKund); } 不使用集合,可以避免使用List
private static void kommandoEtt() {
Kund nyKund = new Kund();
System.out.print("Name: ");
nyKund.setNamn(tangentbord.nextLine());
kundregister.add(nyKund);
}
不使用
集合
,可以避免使用List#contains(Object)
方法将两个相同的对象添加到列表
例如:
List<String> strings = new ArrayList<String>();
if (!list.contains("mystring"))
System.out.println("added string? "+list.add("mystring"));
if (!list.contains("mystring"))
System.out.println("added string? "+list.add("mystring"));
陷阱
上述方法适用于基本Java原语,如String
,Double
,Integer
,。。。等等。如果您有自己的对象,则需要覆盖类的hashCode
和equals
方法。否则,List#contains
方法将根据对象的地址而不是内容来测试相等性
错误的例子:
public class Fraction {
int x, int y;
public Fraction(int x, int y) { this.x=x;this.y=y;}
}
List<Fraction> fractions = new ArrayList<Fraction>();
Fraction f1 = new Fraction(1,2);
if (!fractions.contains(f1))
System.out.println("added fraction? "+fractions.add(f1));
if (!fractions.contains(f1))
System.out.println("added fraction? "+fractions.add(f1));
固定示例:
public class Fraction {
public int x, int y;
public Fraction(int x, int y) { this.x=x;this.y=y;}
@Override
public boolean equals(Object o) {
if (o==null) return false;
if (o==this) return true;
if (!(o instanceof Fraction) return false;
Fraction f = (Fraction) o;
return f.x == x && f.y ==y;
}
}
List<Fraction> fractions = new ArrayList<Fraction>();
Fraction f1 = new Fraction(1,2);
if (!fractions.contains(f1))
System.out.println("added fraction? "+fractions.add(f1));
if (!fractions.contains(f1))
System.out.println("added fraction? "+fractions.add(f1));
不使用
集合
,可以避免使用List#contains(Object)
方法将两个相同的对象添加到列表
例如:
List<String> strings = new ArrayList<String>();
if (!list.contains("mystring"))
System.out.println("added string? "+list.add("mystring"));
if (!list.contains("mystring"))
System.out.println("added string? "+list.add("mystring"));
陷阱
上述方法适用于基本Java原语,如String
,Double
,Integer
,。。。等等。如果您有自己的对象,则需要覆盖类的hashCode
和equals
方法。否则,List#contains
方法将根据对象的地址而不是内容来测试相等性
错误的例子:
public class Fraction {
int x, int y;
public Fraction(int x, int y) { this.x=x;this.y=y;}
}
List<Fraction> fractions = new ArrayList<Fraction>();
Fraction f1 = new Fraction(1,2);
if (!fractions.contains(f1))
System.out.println("added fraction? "+fractions.add(f1));
if (!fractions.contains(f1))
System.out.println("added fraction? "+fractions.add(f1));
固定示例:
public class Fraction {
public int x, int y;
public Fraction(int x, int y) { this.x=x;this.y=y;}
@Override
public boolean equals(Object o) {
if (o==null) return false;
if (o==this) return true;
if (!(o instanceof Fraction) return false;
Fraction f = (Fraction) o;
return f.x == x && f.y ==y;
}
}
List<Fraction> fractions = new ArrayList<Fraction>();
Fraction f1 = new Fraction(1,2);
if (!fractions.contains(f1))
System.out.println("added fraction? "+fractions.add(f1));
if (!fractions.contains(f1))
System.out.println("added fraction? "+fractions.add(f1));
不使用
集合
,可以避免使用List#contains(Object)
方法将两个相同的对象添加到列表
例如:
List<String> strings = new ArrayList<String>();
if (!list.contains("mystring"))
System.out.println("added string? "+list.add("mystring"));
if (!list.contains("mystring"))
System.out.println("added string? "+list.add("mystring"));
陷阱
上述方法适用于基本Java原语,如String
,Double
,Integer
,。。。等等。如果您有自己的对象,则需要覆盖类的hashCode
和equals
方法。否则,List#contains
方法将根据对象的地址而不是内容来测试相等性
错误的例子:
public class Fraction {
int x, int y;
public Fraction(int x, int y) { this.x=x;this.y=y;}
}
List<Fraction> fractions = new ArrayList<Fraction>();
Fraction f1 = new Fraction(1,2);
if (!fractions.contains(f1))
System.out.println("added fraction? "+fractions.add(f1));
if (!fractions.contains(f1))
System.out.println("added fraction? "+fractions.add(f1));
固定示例:
public class Fraction {
public int x, int y;
public Fraction(int x, int y) { this.x=x;this.y=y;}
@Override
public boolean equals(Object o) {
if (o==null) return false;
if (o==this) return true;
if (!(o instanceof Fraction) return false;
Fraction f = (Fraction) o;
return f.x == x && f.y ==y;
}
}
List<Fraction> fractions = new ArrayList<Fraction>();
Fraction f1 = new Fraction(1,2);
if (!fractions.contains(f1))
System.out.println("added fraction? "+fractions.add(f1));
if (!fractions.contains(f1))
System.out.println("added fraction? "+fractions.add(f1));
不使用
集合
,可以避免使用List#contains(Object)
方法将两个相同的对象添加到列表
例如:
List<String> strings = new ArrayList<String>();
if (!list.contains("mystring"))
System.out.println("added string? "+list.add("mystring"));
if (!list.contains("mystring"))
System.out.println("added string? "+list.add("mystring"));
陷阱
上述方法适用于基本Java原语,如String
,Double
,Integer
,。。。等等。如果您有自己的对象,则需要覆盖类的hashCode
和equals
方法。否则,List#contains
方法将根据对象的地址而不是内容来测试相等性
错误的例子:
public class Fraction {
int x, int y;
public Fraction(int x, int y) { this.x=x;this.y=y;}
}
List<Fraction> fractions = new ArrayList<Fraction>();
Fraction f1 = new Fraction(1,2);
if (!fractions.contains(f1))
System.out.println("added fraction? "+fractions.add(f1));
if (!fractions.contains(f1))
System.out.println("added fraction? "+fractions.add(f1));
固定示例:
public class Fraction {
public int x, int y;
public Fraction(int x, int y) { this.x=x;this.y=y;}
@Override
public boolean equals(Object o) {
if (o==null) return false;
if (o==this) return true;
if (!(o instanceof Fraction) return false;
Fraction f = (Fraction) o;
return f.x == x && f.y ==y;
}
}
List<Fraction> fractions = new ArrayList<Fraction>();
Fraction f1 = new Fraction(1,2);
if (!fractions.contains(f1))
System.out.println("added fraction? "+fractions.add(f1));
if (!fractions.contains(f1))
System.out.println("added fraction? "+fractions.add(f1));
另一种向容器中添加没有任何副本的内容的方法是使用集合。如果需要相同的顺序,可以使用LinkedHashSet实现 然后,您需要在类中实现一个equals方法来检查这些项是否是simular 在你的例子中,我猜这个名字可能是不同的值。然后,可以实现一个equals,如下所示:
public boolean equals(Object o) {
// if the object you compare to isn't Kund then they aren't equal.
if(!(o instanceof Kund)) return false;
// return the value of the equals between this object and the object you want
// to check equality on. And use the objects name as the field determining if
// the values are equal.
return this.getName().equalsIgnoreCase((Kund)o.getName());
}
public int hashCode() {
return this.getName().hashCode();
}
您还需要为您的对象实现hashCode函数,以便它在hashset中获得一个特定的位置,供标准java实现查找。另一种向容器添加内容而不使用任何重复项的方法是使用集合。如果需要相同的顺序,可以使用LinkedHashSet实现 然后,您需要在类中实现一个equals方法来检查这些项是否是simular 在你的例子中,我猜这个名字可能是不同的值。然后,可以实现一个equals,如下所示:
public boolean equals(Object o) {
// if the object you compare to isn't Kund then they aren't equal.
if(!(o instanceof Kund)) return false;
// return the value of the equals between this object and the object you want
// to check equality on. And use the objects name as the field determining if
// the values are equal.
return this.getName().equalsIgnoreCase((Kund)o.getName());
}
public int hashCode() {
return this.getName().hashCode();
}
您还需要为您的对象实现hashCode函数,以便它在hashset中获得一个特定的位置,供标准java实现查找。另一种向容器添加内容而不使用任何重复项的方法是使用集合。如果需要相同的顺序,可以使用LinkedHashSet实现 然后,您需要在类中实现一个equals方法来检查这些项是否是simular 在你的例子中,我猜这个名字可能是不同的值。然后,可以实现一个equals,如下所示:
public boolean equals(Object o) {
// if the object you compare to isn't Kund then they aren't equal.
if(!(o instanceof Kund)) return false;
// return the value of the equals between this object and the object you want
// to check equality on. And use the objects name as the field determining if
// the values are equal.
return this.getName().equalsIgnoreCase((Kund)o.getName());
}
public int hashCode() {
return this.getName().hashCode();
}
您还需要为您的对象实现hashCode函数,以便它在hashset中获得一个特定的位置,供标准java实现查找。另一种向容器添加内容而不使用任何重复项的方法是使用集合。如果需要相同的顺序,可以使用LinkedHashSet实现 然后,您需要在类中实现一个equals方法来检查这些项是否是simular 在你的例子中,我猜这个名字可能是不同的值。然后,可以实现一个equals,如下所示:
public boolean equals(Object o) {
// if the object you compare to isn't Kund then they aren't equal.
if(!(o instanceof Kund)) return false;
// return the value of the equals between this object and the object you want
// to check equality on. And use the objects name as the field determining if
// the values are equal.
return this.getName().equalsIgnoreCase((Kund)o.getName());
}
public int hashCode() {
return this.getName().hashCode();
}
您还需要为您的对象实现hashCode函数,以便它在hashset中有一个特定的位置,供标准java实现查找。@Johan Lundström您的问题不是很清楚,很好,您尝试了一些初学者的东西,但请正确解释,以便提供解决方案。迭代并比较,或者查看API文档中的列表,看看是否有类似于
contains
@Johan Lundström的内容。您的问题不是很清楚,很好,您尝试了一些初学者的东西,但请正确解释,以便提供解决方案。迭代并比较,或者查看API文档中的列表,看看是否有类似于contains
@Johan Lundström的内容。您的问题不是很清楚,很好,您尝试了一些初学者的东西,但请正确解释,以便提供解决方案。迭代并比较,或者查看API文档中的列表,看看是否有类似于contains
@Johan Lundström的内容。您的问题不是很清楚,很好,您已经尝试了一些初学者的东西,但请正确解释,以便