根据特定信息删除ArrayList中的对象,java? [重复]

问题描述 投票:0回答:1

以下代码是有关学生的基本信息。所以我的问题是如何从学生的姓氏中删除他(我想删除整个对象)?

   public class Student{
        private String firstName;
        private String lastName;
        private int numberOfIndexCard;

        //  constructors
        public Student(){}

        public Student(String firstName, String lastName, int numberOfIndexCard){                      
                 this.firstName = firstName;
                 this.lastName = lastName;
                 this.numberOfIndexCard = numberOfIndexCard; 
        }
        public static void main(String [] args){
                 ArrayList<Student> students = new ArrayList<Student>("Thomas", "Anderson", 123);
                 ArrayList<Student> students = new ArrayList<Student>("James", "Butler", 456);      
                 ArrayList<Student> students = new ArrayList<Student>("Harry", "Dawson", 789);
    }
}
java arraylist
1个回答
0
投票

您可以使用removeIf

students.removeIf(stu->stu.getLastName().equals("lastname"));
© www.soinside.com 2019 - 2024. All rights reserved.