いくつかの異なる変数を設定および取得する Student という新しいクラスを作成するプログラムを作成しています。正しく出力される配列要素を ArrayList に格納しようとすると、代わりにハッシュ コードが取得されます。1 つまたは 2 つの小さな変更を加えるだけで問題が解決すると確信していますが、どうしようもありません。すべての提案を歓迎します、ありがとう!
主要:
Student st = new Student();
Student();
}
public static void Student() {
System.out.println("Enter the data for student 1 :");
Student st = new Student();
Scanner keyboard = new Scanner(System.in);
String name = keyboard.next();
double GPA = keyboard.nextDouble();
boolean enrolled = keyboard.nextBoolean();
String date = keyboard.next();
for (int i = 0; i<5; i++){
Student newStudent = new Student();
newStudent.setName(name);
newStudent.setGPA(GPA);
newStudent.setEnrolled(enrolled);
newStudent.setDate(date);
ArrayList <Student> strList = new ArrayList<Student>();
Student student[] = new Student[5];
newStudent.getName();
newStudent.getGPA();
newStudent.getEnrolled();
newStudent.getDate();
newStudent.toString();
student[i] = newStudent;
ArrayList myList = new ArrayList();
myList.add(student[i]);
System.out.println(student[i].getName());
System.out.print(student[i].getGPA());
System.out.print(student[i].getEnrolled());
System.out.print(student[i].getDate());
System.out.println(myList);
}
}
学生クラス :
public class Student {
private String name;
private double gpa;
private boolean enrolled;
private String date;
public Student() {
this.name = "";
this.gpa = 0.0;
this.enrolled = false;
this.date = "none";
}
public Student(String name1) {
this.name = name1;
this.gpa = 0.0;
this.enrolled = false;
this.date = "none";
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setGPA(double gpa) {
this.gpa = gpa;
}
public double getGPA() {
return this.gpa;
}
public void setEnrolled(boolean enrolled) {
this.enrolled = enrolled;
}
public boolean getEnrolled() {
return this.enrolled;
}
public void setDate(String date) {
this.date = date;
}
public String getDate(){
return this.date;
}
}