[英]How to access an array and assign data to each object in Java?
I'm trying to figure out how to access the Student Array
class in order to create four entries for each Student
object, but I'm not sure how to do so, while also allowing the program to create more than just one Student
. 我试图弄清楚如何访问
Student Array
类,以便为每个Student
对象创建四个条目,但是我不确定该如何做,同时还允许该程序创建一个以上的Student
。
public class ClassRoster<T> {
public static void main(String[]args) {
ClassRoster<Student> classroster = new ClassRoster<Student>();
Scanner keyboard = new Scanner(System.in);
System.out.println("Add/Drop/Search?");
String action = keyboard.nextLine();
boolean done = false;
Object temp, c, d, e;
int fresh, soph, jun, sen;
Student test = new Student();
while(!done) {
if(action.equalsIgnoreCase("Add"))
{
int counter = 0;
System.out.print("Enter Student ID");
temp = test.setID(keyboard.nextInt());
System.out.println("First name?");
c = test.setFirstName(keyboard.nextLine());
System.out.println("Last name?");
d = test.setLastName(keyboard.nextLine());
System.out.println("Academic Level?");
e = test.setLevel(keyboard.nextLine());
...
}
And I have another class called Student
, where there are four different data entries (ID, FirstName, LastName, Academic Level). 我还有另一个班级
Student
,其中有四个不同的数据条目(ID,名字,姓氏,学术水平)。
I'm not sure how to access the object which I have created in the correct way. 我不确定如何访问以正确方式创建的对象。 It just gives me an error in this
Driver
class, and I don't know how to correctly access the array bag. 它只是给我这个
Driver
类错误,我不知道如何正确访问数组包。
but I'm not sure how to do so while also allowing the program to create more than just one Student
但我不确定该怎么做,同时还允许该程序创建多个学生
Currently you are only creating one specific instance of student with Student test = new Student();
目前,您仅使用
Student test = new Student();
创建一个学生的特定实例Student test = new Student();
To actually create more than one student, you will have to iterate the whole process of reading all four data entries (ID, FirstName, LastName, Academic Level). 要实际创建一个以上的学生,您将必须遍历读取所有四个数据条目(ID,名字,姓氏,学术水平)的整个过程。 Instead of having to initialize the fields (your four data entries) with specific set methods, I would recommend you letting the Student class initialize them with the class constructor.
我建议您不必让Student类使用类构造函数对其进行初始化,而不必使用特定的set方法来初始化字段(您的四个数据条目)。 Meaning the Student class should look something like this:
意味着Student类应该看起来像这样:
public class Student{
private final int ID;
private final String firstname;
private final String lastname;
private String level;
public Student(int ID, String firstname, String lastname, String level){
this.ID = ID;
this.firstname = firstname;
this.lastname = lastname;
this.level = level;
}
ID, firstname and lastname are set to final as you foresee them not to change. ID,名和姓将设置为最终名称,因为您预计它们不会更改。 However the academic level is ought to change and therefore is not set to final.
但是,学术水平应该改变,因此不能设置为最终水平。 Now that you have set up a constructor for your Student class, we can get to how to allow the program to insert multiple students at once.
现在,您已经为学生类设置了一个构造函数,我们现在可以了解如何允许该程序一次插入多个学生。
public static void main(String[]args) {
ClassRoster<Student> classroster = new ClassRoster<Student>();
Scanner keyboard = new Scanner(System.in);
System.out.println("Add/Drop/Search?");
String action = keyboard.nextLine();
boolean done = false;
while(!done) {
if(action.equalsIgnoreCase("Add")) {
System.out.print("Enter Student ID");
int ID = keyboard.nextInt();
System.out.println("First name?");
String firstname = keyboard.nextLine();
System.out.println("Last name?");
String lastname = keyboard.nextLine();
System.out.println("Academic Level?");
String level = keyboard.nextLine();
Student student = new Student(ID, firstname, lastname, level);
//we have now created a new instance of Student, now we have to save it in your classroster
classroster.add(student);
}
System.out.println("Next action?");
action = keyboard.nextLine();
if(action.equals("done") done = true; //if you write 'done', your loop will finish executing
}
I don't know about your implementation of classroster, but I assume you have implemented it with some kind of list or map, which is why I call the add(Student s) method after creating an instance of Student. 我不知道您如何实现classroster,但我认为您已经使用某种列表或映射实现了它,这就是为什么在创建Student实例之后调用add(Student s)方法的原因。 To actually then access all students, you will have to implement a method in classroster that returns the saved list of classroster and then iterate through the returned list in the main loop.
要实际访问所有学生,您将必须在classroster中实现一个方法,该方法返回已保存的classroster列表,然后在主循环中遍历返回的列表。 To actually see what the students look like, you will also have to implement methods for the student instances to for example print out their full names.
要实际查看学生的样子,您还必须实现学生实例的方法,例如打印出他们的全名。
I see that you are having a little trouble with arrays, maps and lists as you don't know how to access your students yet. 我发现您在使用数组,地图和列表时遇到了一些麻烦,因为您尚不知道如何访问学生。 I recommend you reading up on the difference between these three data structure types and simply try to implement them in a small example to see how they work.
我建议您阅读这三种数据结构类型之间的区别,并尝试在一个小示例中尝试实现它们,以了解它们如何工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.