gpt4 book ai didi

java - 询问非基元数组列表

转载 作者:太空宇宙 更新时间:2023-11-04 09:27:57 25 4
gpt4 key购买 nike

我已经创建了ArrayList。我如何使用此列表从类中获取方法或属性。我尝试过,但无法找到任何解决方案。

我尝试进入数组列表中的元素并获取一些属性,但我不能。

    public static void printOptions() {
System.out.println("Welcome to our university!");
System.out.println("Operations:");
System.out.println("1- College");System.out.println("a) Number of Departments");System.out.println("b) Number of Courses");System.out.println("c) Number of Professors");System.out.println("d) Number of Students");System.out.println("e) Report");
System.out.println("2- Department");System.out.println("a) New");System.out.println("b) Number of Courses");System.out.println("c) Number of Students");System.out.println("d) Is Full");System.out.println("e) Enroll");System.out.println("f) Report");
System.out.println("3- Course");System.out.println("a) New");System.out.println("b) Number of Students");System.out.println("c) Assign");System.out.println("d) Is assigned");System.out.println("e) Professor Name");System.out.println("f) Is Full");System.out.println("g) Enroll");System.out.println("h) Report");
System.out.println("4- Professor");System.out.println("a) New");System.out.println("b) Display Salary");System.out.println("c) Get Raise");System.out.println("d) Report");
System.out.println("5- Student");System.out.println("a) New");System.out.println("b) Report");
System.out.println("6- Quit");
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

printOptions() ;
List<Department> departmentList;departmentList = new ArrayList<>();
List<Course> courseList ;courseList = new ArrayList<>();
List<Professor> proffList = new ArrayList<>() ;
List<Student> studentList;studentList = new ArrayList<>() ;



Scanner in = new Scanner(System.in) ;
int d = 0 , c = 0 , p = 0 , s=0 ;
College AinShams = new College() ;
while (true){

String option = in.nextLine() ;
if(!"6".equals(option)) {
if ("2a".equals(option)) { // Define new department
System.out.println("Department Name:");
String depName = in.nextLine() ;
System.out.println("Department Description:");
String depDescripe = in.nextLine() ;
System.out.println("Department Max Students:");
int max_num = in.nextInt() ;
in.nextLine() ;
Department Department_Name = new Department(depName, depDescripe, max_num);

// departmentList.add(Department_Name);
departmentList.add(d, Department_Name);
d++ ;
AinShams.setDepart(departmentList);

}

else if ("4a".equalsIgnoreCase(option)) {//new proff
System.out.println("Professor Firstname:");
String firstName = in.nextLine() ;
System.out.println("Professor Lastname:");
String lastName = in.nextLine();
System.out.println("Professor telephone:");
String telephone = in.nextLine();
System.out.println("Professor address:");
String address = in.nextLine();
System.out.println("Professor salary:");
double salary = in.nextDouble() ;
Professor proff = new Professor(firstName, lastName, telephone, address, salary);
proffList.add(p,proff) ;
p++ ;
AinShams.setProf(proffList);
}

else if ("2e".equalsIgnoreCase(option)) {//add student in department
System.out.println("Department:");
String dep= in.nextLine() ;
System.out.println("Student:");
String stu= in.nextLine();

// System.out.println(AinShams.getDepart());
AinShams.getDepart().
/*for (int i = 0; i < AinShams.getDepart().size(); i++) {
}*/
}
System.out.println("============");
System.out.println("Enter Operation");
System.out.println("============");
}else {break ;}


}

}

}

在条件(2e)中,我需要获取我在数组List中分配的类中的方法和属性

最佳答案

AinShams.getDepart() 引用您定义为数据类型 College 的对象:

College AinShams = new College(); 

学院代码的其余部分不在该片段中,因此我无法判断 .getDepart() 方法是否存在。不管怎样,College 都不是一个 ArrayList。

如果你想访问 ArrayList 中对象的方法和字段,类似下面的内容就可以了。作为示例,我采用名为 Departmentlist 的 ArrayList,并使用 get() 方法返回该列表中的第 0 个元素。假设该类的第 0 个元素是 Department 类型的对象,.name 要求提供 name 字段(再次假设该 name 变量存在于 Department 类中)。 .getName() 是获取名称值的更好方法,但需要您在 Department 类中编写此方法。

departmentlist.get(0).name
departmentlist.get(0).getName()

顺便说一句,请考虑使用“\n”换行键格式化输出,以减少代码顶部的一些“System.out.println()”困惑。尝试使用 1 个 system.out.println 调用来打印两行文本,这两个示例:

System.out.println("Welcome to our university!" + "\n" + "Operations:");
System.out.println("Welcome to our university! \nOperations:");

关于java - 询问非基元数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57449477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com