如何查找 arraylist 是否包含类字段中的值?
我有课:
static class AnotherClass {
int number;
String isPrime;
public int getNumber() {
return number;
}
public String getPrime() {
return isPrime;
}
public void setNumber(int number) {
this.number = number;
}
public void setPrime(String prime) {
isPrime = prime;
}
}
在主课上我有:
List<AnotherClass> listx = new ArrayList<AnotherClass>();//just a arraylist
for (int z = 0; z < howManyQuestions; z++) {//in loop i add class with fields
AnotherClass classx = new AnotherClass();
int valuex = Integer.parseInt(keyboardkey.readLine());
classx.setNumber(valuex);//save value in this class
String answer = Check(valuex);//i just get here string answer YES NO
classx.setPrime(answer);
listx.add(classx);//and i add this two fields of class to list
System.out.println();
}
INPUT: (i add value and check if it was input before) 3 4 3 OUTPUT NO NO YES How can i check if, for example value "3" is containing by list?
I have class:
static class AnotherClass {
int number;
String isPrime;
public int getNumber() {
return number;
}
public String getPrime() {
return isPrime;
}
public void setNumber(int number) {
this.number = number;
}
public void setPrime(String prime) {
isPrime = prime;
}
}
and in main class i have:
List<AnotherClass> listx = new ArrayList<AnotherClass>();//just a arraylist
for (int z = 0; z < howManyQuestions; z++) {//in loop i add class with fields
AnotherClass classx = new AnotherClass();
int valuex = Integer.parseInt(keyboardkey.readLine());
classx.setNumber(valuex);//save value in this class
String answer = Check(valuex);//i just get here string answer YES NO
classx.setPrime(answer);
listx.add(classx);//and i add this two fields of class to list
System.out.println();
}
INPUT: (i add value and check if it was input before) 3 4 3 OUTPUT NO NO YES How can i check if, for example value "3" is containing by list?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
1 AnotherClass 必须实现 equals()(并相应地实现 hashCode())。
2 使用 listx 中的 contains(Object o) 方法。
1 AnotherClass must implement equals() (and implement hashCode() accordingly).
2 Use method contains(Object o) from listx.
一些注意事项 -
你的类不需要静态。如果您要声明内部类,那么这很有用。
您有一个容器类,其中包含一个依赖于 int 的字符串以及 int。在类中进行检查会更惯用,例如,
如果您正在寻找“包含”功能,则可能会使用 HashSet 或 LinkedHashSet(如果您想保留排序)。如果您想对创建的类执行此操作,则需要实现 hashCode() 方法来告诉 hashSet 如何知道它是否具有重复值。
或者您可以迭代您的列表。
A Few notes -
your class doesn't require static. That has a use if you're declaring an inner class.
You have a container class holding a string that's dependant on an int, as well as the int. It'd be more idiomatic to have the check inside your class, e.g.
If you're looking for "contains" functionality, you'd probably use a HashSet, or a LinkedHashSet( if you want to preserve the ordering ). If you want to do this with your created class you'll need to implement a hashCode() method to tell the hashSet how to know if it has a duplicate value.
Or you can just iterate over your list.
您必须为
AnotherClass
实现equals()
。默认的equals()
比较同一性而不是值相等性。List.contains()
的 javadoc 表示:如果此列表包含指定元素,则返回 true。更正式地说,当且仅当此列表包含至少一个元素 e 且满足
(o==null ? e==null : o.equals(e))
时,才返回 true。You have to implement
equals()
forAnotherClass
. The defaultequals()
compares identity instead of value equality.The javadoc for
List.contains()
says:Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that
(o==null ? e==null : o.equals(e))
.