java - 如何在java中匹配另一个类中的arraylist数据

标签 java class arraylist

我开发了一个添加学生的学生类,在主类中我使用 arraylist 来显示所有已添加的学生。现在我做了一个报名类(class),我想将进入报名类(class)的ID与已添加的学生匹配吗?我遇到了问题,有人可以告诉我该怎么做吗...将我的代码放在下面

这里是勉类-

import java.util.*;


public class StudentRecordSystem {

static ArrayList<Student> list = new ArrayList<Student>();

public static void main(String[] args) {
    // TODO Auto-generated method stub


    String option;
    String option1 = "a";//initializing the option a from the main menu
    String option2 = "b";//initializing the option b from the main menu
    String option3 = "c";//initializing the option c from the main menu
    String option4 = "d";//initializing the option d from the main menu
    String option5 = "e";//initializing the option e from the main menu
    String option6 = "f";//initializing the option f from the main menu
    String option7 = "g";//initializing the option g from the main menu
    for(int i=1; i<2; i++)
    {
        System.out.println("========== Main Menu ==========");
        System.out.println("a)Student Admin \nb)Course Admin \nc)Staff Admin \nd)Exit");//printing the main menu options
        System.out.println("Your option: ");//asking the user for option to choose
        Scanner inputOption = new Scanner(System.in);
        option = inputOption.nextLine();//Taking user option

        if(option.equals(option1))
        {
            for(int l=1; l<2; l++){


                System.out.println("========== Student Admin Menu ==========");
                System.out.println("a)Add new Student \nb)List Enrolment Details \nc)Enrol Student \nd)Assign Exemption To a Student \ne)Process Payment \nf)Generate Report \ng)Go Back");//printing the main menu options
                System.out.println("Your option: ");//asking the user for option to choose
                option = inputOption.nextLine();//Taking user option
                if(option.equals(option1))
                {
                    Student st = new Student();
                    list.add(st);
                    l--;//continues the loop
                }
                if(option.equals(option2))
                {
                    for (Student st : list) {
                        System.out.println(st.toString());
                    }
                    l--;//continues the loop
                }
                if(option.equals(option3))
                {
                    String[] courseName = {"Java Programming","Object Oriented Design","Software Testing","J2EE","Software Architecture","Design Patterns"};
                    Course c = new Course(courseName);
                    //c.getCourseName();
                    System.out.println(Arrays.toString(c.getCourseName()));
                }
                if(option.equals(option4))
                {
                    System.out.println("Will assign exemption to a  student");
                }
                if(option.equals(option5))
                {
                    System.out.println("Will process payment for the student");
                }
                if(option.equals(option6))
                {   
                    System.out.println("Will Generate Report");
                }
            }
            if(option.equals(option7))
            {
                i--;//continues the loop
            }

        }
        else if(option.equals(option2))
        {
            System.out.println("Will go to course class");
            i--;//continues the loop
        }
        else if(option.equals(option3))
        {
            System.out.println("Will go to staff class");
            i--;//continues the loop
        }
        else if(option.equals(option4))
        {
            System.out.println("Thank You For Using This Program");
            System.exit(0);
        }
        else
        {
            System.out.println("Error:  The selected option is not Recognised. Try Again...");//Error message if wrong option has been entered
            i--;//continues the loop
        }
    }

}

}

这是学生类-

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

public class Student {

private String studentName;
private String studentId;
private String dob;
private Scanner scan;

//Default Constructor
public Student()
{
    scan = new Scanner(System.in);

    System.out.print("Student Name:");
    studentName = scan.next();//Taking Student Name

    System.out.print("Student ID:");
    studentId = scan.next();//Taking Student ID

    System.out.print("Student Date Of Birth(DD/MM/YY):");
    dob = scan.next();//Taking Student Date of Birth

}

//Constructor
public Student(String newStudentName, String newStudentId, String newDob)
{
    this.studentName = newStudentName;
    this.studentId = newStudentId;
    this.dob = newDob;
}

//Setter Methods
public void setStudentName(String newStudentName)
{
    studentName = newStudentName;
}

public void setStudentId(String newStudentId)
{
    studentId = newStudentId;
}

public void setDob(String newDob)
{
    dob = newDob;
}

//Getter Methods
public String getStudentName()
{
    return studentName;
}

public String getStudentId()
{
    return studentId;
}

public String getDob()
{
    return dob;
}

public String toString() {
    return getStudentName() + "\t" + getStudentId() + "\t" + getDob();
}

}

这是入学类(class) -

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


public class Enrollment {

private String studentId;
private Scanner scan;



public Enrollment()
{
    scan = new Scanner(System.in);

    System.out.println("Please Enter The Student ID:");
    studentId = scan.next();


}



}

最佳答案

Enrollment类中为studentId添加getter

public String getStudentId(){
    return studentId;
}

在主类中,初始化 list 后创建 Enrollment 对象

Enrollment enroll = new Enrollment();

迭代ArrayList以查找匹配项:

boolean isfound = false;
for(int i = 0; i < list.size(); i++){
    if(list.get(i).getStudentId().equals(enroll.getStudentId()){
        isfound = true;
    }
}
if(isfound){
    System.out.println("Student record found!");
}
else{
     System.out.println("No match found!");
}

关于java - 如何在java中匹配另一个类中的arraylist数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23235475/

相关文章:

java - 使用 Java、Eclipse 创建 RESTful Web 服务的教程。 Apache Tomcat,不工作

java - 如何创建一个为测试脚本传递参数的 webdriver 函数?

Java Class.forName() 不起作用,从属性文件加载类字符串

java - 在 Android 中,如何检查 webview 中加载的 URL 是否是图像?

c++ - 我正在尝试用 vector 拍摄

C++ 虚拟继承不起作用,如何使用多个父级成员?

java - 其他类能够访问对象类型的数组列表

java - 确定文本文件长度的最有效方法是什么?

java - 一个对象能知道它的方法是从哪个对象调用的吗?

java - 将 foreach 循环与另一个类中的方法结合使用 (Java)