简体   繁体   English

如何从 arraylist 中搜索和删除 object?

[英]How to search and remove an object from an arraylist?

This is a follow up question from my previous post:这是我之前帖子的后续问题:

How to separate different values in 2-d array 如何分隔二维数组中的不同值

I now need to delete specific object from the array according to the criteria, eg, name and age, then save the result in file.我现在需要根据条件(例如姓名和年龄)从数组中删除特定的 object,然后将结果保存到文件中。 I have read some examples using iterator but didn't follow well.我已经阅读了一些使用迭代器的示例,但没有很好地遵循。

Original data in txt file: txt文件中的原始数据:

G Steve 23 Biology 
U Julia 19 Music Flute Sophomore 
G David 25 Math

Below is the method I used to create the arraylist while reading the data from txt file以下是我在从 txt 文件读取数据时创建 arraylist 的方法

ArrayList<Student> studentList = new ArrayList<>();
Student s = new Undergraduate(type,name,age,major,year);
studentList.add(s);
Student s = new Graduate(type,name,age, major);
studentList.add(s);

for (Student d:studentList) {
    System.out.println(d.toString());
}

Thanks in advance!提前致谢!

If you are using Java 8 you could take the advantage of Streaming.如果您使用的是 Java 8,您可以利用 Streaming。 For example your code could look like:例如,您的代码可能如下所示:

List<Student> myFilteredList = studentList.stream()
  .filter(student -> !student.getName().equals("Julia"))
  .filter(student -> student.getAge() > 23)
  .collect(Collectors.toList())

If you are not used to lambda expression I think it would be a great idea to take a look into that topic.如果您不习惯 lambda 表达式,我认为查看该主题将是一个好主意。

To remove a an object from a list you can use removeIf with a condition, below example removes any student older than 22 of type "G"要从列表中删除 object,您可以使用带有条件的removeIf ,下面的示例删除任何年龄超过 22 岁的“G”类型的学生

studentList.removeIf(s -> s.age > 22 && s.type.equals("G"));

I tried several methods finally got this:我尝试了几种方法终于得到了这个:

                                System.out.println("Enter Student name: ");
                                System.out.println("Enter StudentID: ");
                                String dname = sc.next();
                                int dage = sc.nextInt();



                                for (Student s:studentList){
                                    if (s.getName().equals(dname)&&s.getAge()>dage) {
                                        System.out.println();
                                        studentList.remove(s);
                                        System.out.println("You removed: "+s.getName());
                                        System.out.println();
                                        break;
                                    } else {
                                        System.out.println();
                                        System.out.println("This Student doesn't exist");
                                        System.out.println();
                                        break;
                                    }
                                }

It runs but always says "The Student doesn't exist".它运行但总是说“学生不存在”。 Is it that the "getName" not working (I assume it calls the "name" string from the constructor in the Student class?) Anybody can help?是不是“getName”不起作用(我假设它从 Student class 的构造函数中调用“name”字符串?)有人可以帮忙吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM