简体   繁体   中英

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. I have read some examples using iterator but didn't follow well.

Original data in txt file:

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

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. 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.

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"

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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