简体   繁体   English

在数组列表中搜索特定对象并打印数组列表中的所有对象

[英]searching for a particular object in array list and printing all the objects in array list

I have to implement a java application that demonstrates the use of array list of objects. 我必须实现一个Java应用程序来演示对象数组列表的用法。 I am using Students and I have a StudentTest class. 我正在使用学生,并且有一个StudentTest班。 I should be able to enter in details(which I have done). 我应该能够输入详细信息(我已经完成了)。 I'm just unsure how to search for a particular object in the array list by Student Number and also to print the array list. 我只是不确定如何通过学生编号在数组列表中搜索特定对象,以及如何打印数组列表。 Any hints would be great thanks. 任何提示将非常感谢。

code so far: 到目前为止的代码:

package student;

public class Student {

    private String studentName;
    private String studentNo;
    private String email;
    private int year;

    public Student() {
        this.studentName = null;
        this.studentNo = null;
        this.email = null;
        this.year = -1;
    }

    public Student(String nName, String nNum, String nEmail, int nYr) {
        this.studentName = nName;
        this.studentNo = nNum;
        this.email = nEmail;
        this.year = nYr;
    }

    public void setStudentName(String newStudentName) {
        this.studentName = newStudentName;
    }

    public void setStudentNo(String newStudentNo) {
        this.studentNo = newStudentNo;
    }

    public void setEmail(String newEmail) {
        this.email = newEmail;
    }

    public void setYear(int newYear) {
        this.year = newYear;
    }

    public String getStudentName() {
        return studentName;
    }

    public String getStudentNo() {
        return studentNo;
    }

    public String getEmail() {
        return email;
    }

    public int getYear() {
        return year;
    }

}
package student;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class studentTest {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);


        List<Student> Students = new ArrayList();


        System.out.println("Enter number of students");
        int countStudents = input.nextInt();

        for (int i = 0; i < countStudents; i++) {
            Student student1 = new Student();
            System.out.println("Enter details for student: " + (i + 1));

            System.out.println("Enter name: ");
            student1.setStudentName(input.next());

            System.out.println("Enter Number: ");
            student1.setStudentNo(input.next());

            System.out.println("Enter email: ");
            student1.setEmail(input.next());

            System.out.println("Enter year: ");
            student1.setYear(input.nextInt());
            Students.add(student1);



        }
    }
}

You need to implement a equals() method in your Student class, that will return true if 2 students have same studentNo , else false . 您需要在Student类中实现equals()方法,如果2个学生具有相同的studentNo ,则返回true,否则返回false

@Override
public boolean equals(Object ob) {

    if (!(ob instanceof Student)) { 
        return false;
    }

    return ob.getStudentNo().equals(this.getStudentNo());
}

Now, you can just use contains method to check whether a student is in the List or not. 现在,您可以只使用contains方法来检查student是否在List

UPDATE : - 更新 :-

Also, you need to override hashcode method, whenever you are overriding equals method, to maintain the general contract between them. 同样,无论何时overriding equals方法,都需要重写hashcode方法,以保持它们之间的常规协定。 So override hashcode that would calculate the hashcode of your instances based only on the fields on which you are comparing your objects 所以覆盖hashcode ,将计算hashcode你的instances仅基于在其上比较你的域objects

So, you need to calculate hashcode using studentNo . 因此,您需要使用studentNo来计算hashcode

@Override
public int hashcode() {
    return this.getStudentNo().hashCode(); 
}
  • HashCode calculating algorithm should be such that, if two instances are equal, their hashcode should also be equals HashCode的计算algorithm应为:如果两个实例相等,则其哈希码也应等于
  • It should return same value for same instance everytime, of course when the instance has not been modified. 当然,在未修改实例的情况下,它每次都应为同一实例返回相同的值。
  • Two different instances can have the same hashcode 两个不同的实例可以具有相同的哈希码

As suggested by @hyde in the comment : - 正如@hyde在评论中所建议的 :-

If this is just too much work for you, then you can just have a different method, that does the same task as equals method, say, checkEquals() , and ignore overriding equals() method. 如果这对您来说太麻烦了,那么您可以使用其他方法,该方法与equals方法执行相同的任务,例如checkEquals() ,而ignore覆盖equals()方法。 But in that case, you would have to do iteration on your List and for each list element, invoke this method with the student to compare. 但是在这种情况下,您将必须对List进行iteration ,并且对于每个list元素,请student调用此method进行比较。

for(Student student: studentList) {
    if (student.checkEquals(myStudent)) {
        System.out.println("True");
    }
}

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

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