如何在java中匹配另一个类中的arraylist数据
我开发了一个添加学生的学生类,在主类中,我使用arraylist显示所有已添加的学生。现在我做了一个注册班,我想匹配已输入注册班的ID,它是否匹配已添加的学生。我有问题,谁能告诉我怎么做。。。把我的代码放在下面 这是面班-如何在java中匹配另一个类中的arraylist数据,java,class,arraylist,Java,Class,Arraylist,我开发了一个添加学生的学生类,在主类中,我使用arraylist显示所有已添加的学生。现在我做了一个注册班,我想匹配已输入注册班的ID,它是否匹配已添加的学生。我有问题,谁能告诉我怎么做。。。把我的代码放在下面 这是面班- import java.util.*; public class StudentRecordSystem { static ArrayList<Student> list = new ArrayList<Student>(); public s
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 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();
}
}
在主创建选项中,用户希望按ID搜索学生。 并创建EnrollmentClass的对象,并将Arraylist传递给构造函数。
在Enrollment类构造函数中,您需要迭代每个arrayList,直到找到id。在
Enrollment
类中为studentId
添加getter
public String getStudentId(){
return studentId;
}
在主类中,在初始化列表
后创建注册
对象
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!");
}
boolean isfound=false;
对于(int i=0;i
当注册对象被创建时?它应该有学生列表,对吗?@user3515895不是注册
类应该是一个对象定义吗?在你的主类中进行搜索可以,但我也放了一个else语句来显示是否没有记录或是否没有找到记录。但它只是打印消息匹配,不打印else语句
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!");
}