当它等于arraylist字符串时,我需要删除pojo类arraylist项。
下面我发布了代码:
Sample.java:
import java.util.ArrayList;
public class Sample {
public static void main(String args[]) {
ArrayList<String> arrStr;
ArrayList<Student> arrStudent;
arrStr = new ArrayList<String>();
arrStudent = new ArrayList<Student>();
arrStr.add("100");
arrStr.add("150");
arrStr.add("200");
arrStr.add("250");
arrStr.add("300");
arrStudent.add(new Student("Max", "Get Data ", "140"));
arrStudent.add(new Student("Max", "Get Data ", "150"));
arrStudent.add(new Student("Max", "Get Data ", "160"));
arrStudent.add(new Student("Max", "Get Data ", "200"));
arrStudent.add(new Student("Max", "Get Data ", "230"));
for (String item : arrStr) {
System.out.println("item :" +item);
System.out.println("ArrListCheck" +arrStudent.toString());
if (arrStudent.contains(item)) {
arrStudent.remove(item);
} else {
System.out.println("Else Called");
}
}
}
}
Student.java:
public class Student {
public String name;
public String description;
public String id;
public Student(String name, String description, String id) {
this.name = name;
this.description = description;
this.id = id;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public String getId() {
return id;
}
@Override
public String toString() {
String str = name +""+description+""+id;
return str;
}
}
输出:
item :100
ArrListCheck[MaxGet Data 140, MaxGet Data 150, MaxGet Data 160, MaxGet Data 200, MaxGet Data 230]
Else Called
item :150
ArrListCheck[MaxGet Data 140, MaxGet Data 150, MaxGet Data 160, MaxGet Data 200, MaxGet Data 230]
Else Called
item :200
ArrListCheck[MaxGet Data 140, MaxGet Data 150, MaxGet Data 160, MaxGet Data 200, MaxGet Data 230]
Else Called
item :250
ArrListCheck[MaxGet Data 140, MaxGet Data 150, MaxGet Data 160, MaxGet Data 200, MaxGet Data 230]
Else Called
item :300
ArrListCheck[MaxGet Data 140, MaxGet Data 150, MaxGet Data 160, MaxGet Data 200, MaxGet Data 230]
Else Called
正如你在上面的代码中看到的,它总是调用else。我不知道如何在arraylist pojo类中使用arraylist字符串中的两个id相等,然后我必须根据id.Anyone删除arraylist pojo类项目可以帮助我。
答案 0 :(得分:1)
您可以迭代ArrayList<Student>
而不是遍历ArrayList<String>
:
for (int i = 0; i < arrStudent.size(); i++) {
Student student = arrStudent.get(i);
if (arrStr.contains(student.getID())) {
arrStudent.remove(i);
i--;
} else {
System.out.println("Else Called");
}
}
答案 1 :(得分:0)
你的item
是一个遍历arrStr
的valeus的字符串。
然后尝试将字符串与Student对象进行比较:
if (arrStudent.contains(item))