Java ArrayList 检查数据是否已经存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30002483/
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
ArrayList check if data already exists
提问by Oreo
Just want to check if data already exist(s) or not in ArrayList, but everytime getting : Not Exist(s) whereas data exists in ArrayList, and as a result i am getting duplicate records in a list..
只是想检查数据是否已经存在于 ArrayList 中,但每次获取:不存在而数据存在于 ArrayList 中,因此我在列表中获取重复记录..
DataArrayList.secondArraylist.add(new Second(actorList.get(position).getName(), actorList.get(position).getImage()));
System.out.println(DataArrayList.secondArraylist.size());
String strName = actorList.get(position).getName().toString();
Log.d("name:", strName);
for(int i=0; i<DataArrayList.secondArraylist.size(); i++)
{
if(DataArrayList.secondArraylist.get(i).getName().equals(strName)) {
System.out.println(DataArrayList.secondArraylist.get(i).getName());
Toast.makeText(context, "Exist(s)", Toast.LENGTH_SHORT);
}
else {
Toast.makeText(context, "Not Exist(s)", Toast.LENGTH_SHORT);
}
}
Problem:
问题:
Not getting any Toast message, which i am using to indicate that "Data Exists" or "Not"
没有收到任何 Toast 消息,我用它来表示“数据存在”或“不存在”
Finally:
最后:
I would like to add item to arraylist if already not exist(s), else want to show Toast that item already exist(s)
如果不存在,我想将项目添加到数组列表,否则想向 Toast 显示该项目已经存在
采纳答案by pathfinderelite
You are comparing your list object DataArrayList.secondArraylist
with a String
您正在将列表对象DataArrayList.secondArraylist
与String
if (DataArrayList.secondArraylist.equals(strName)) {
That will always return false
. Instead, create a method that loops through the list and checks the strName
against the name of the Second
objects that are stored in the list.
那总会回来的false
。相反,创建一个循环遍历列表的方法,并strName
根据Second
存储在列表中的对象的名称检查。
boolean contains(ArrayList<Second> list, String name) {
for (Second item : list) {
if (item.getName().equals(name)) {
return true;
}
}
return false;
}
Then use it
然后使用它
if (contains(DataArrayList.secondArraylist, strName)) {
System.out.println("Data Exist(s)");
} else {
System.out.println("Not Exist(s)");
}
回答by Md. Nasir Uddin Bhuiyan
You need to use indexOf()
while checking a value in ArrayList
您需要indexOf()
在检查 ArrayList 中的值时使用
if (DataArrayList.secondArraylist.indexOf(strName)!=-1) {
System.out.println("Data Exist(s)");
} else {
System.out.println("Not Exist(s)");
}
回答by Gisonrg
As the strName
you are checking is a property of your Object Second
, you cannot check it directly using method of the ArrayList
. The easiest way would be loop through the ArrayList DataArrayList.secondArraylist
, get each Second
object contained in the list, then check if strName
is equal to that property of your Second
object.
由于strName
您正在检查的是 Object 的一个属性Second
,因此您不能直接使用 的方法检查它ArrayList
。最简单的方法是遍历 ArrayList DataArrayList.secondArraylist
,获取Second
列表中包含的每个对象,然后检查是否strName
等于Second
对象的该属性。
Not sure how you implement the Second
object, but you should get the corresponding String from each Second
object then compare it with the strName
.
不确定您如何实现该Second
对象,但您应该从每个Second
对象中获取相应的 String,然后将其与strName
.
回答by SweetWisher ツ
The contains method calls the equals method with signature equals(Object), so you requires to override this method in your POJO class.
contains 方法调用具有签名 equals(Object) 的 equals 方法,因此您需要在 POJO 类中覆盖此方法。
If you override equals(Object) then you should also override hashcode()
如果你覆盖 equals(Object) 那么你也应该覆盖 hashcode()
Code snippet :
代码片段:
@Override
public String toString() {
return getName();
}
@Override
public boolean equals(Object obj) {
return !super.equals(obj);
}
@Override
public int hashCode() {
return getName().hashCode();
}
Edit :
编辑 :
Moreover, The contains method is used for Object.So, your condition should look like this :
此外,包含方法用于对象。所以,你的情况应该是这样的:
(DataArrayList.secondArraylist.contains(actorList.get(position)))
Final code :
最终代码:
if (DataArrayList.secondArraylist.contains(actorList.get(position))) {
System.out.println("Data Exist(s)");
} else {
System.out.println("Not Exist(s)");
}