关闭 JFrame 后清空 ArrayList
empty ArrayList after closeing JFrame
我有不同的 class,但只有一个 public ArrayList,每个 class 都可以访问它。我的第一个 class 叫做 User,看起来像这样
public class User{
private String fName, lName, username, password;
public ArrayList<Student> students = new ArrayList<>();
public User(String username, String fName, String lName, String password){
this.username = username;
this. fName = fName;
this.lName = lName;
this.password = password
}
//Getter and Setter Methods here
//here my own methods
public void addStudent(Student student){
students.add(student);
}
}
我的第二个 class 叫做 Student,看起来像这样
public class Student extends User{
private String username;
private String fName;
private String lName;
private String password;
private Course course;
public Student(String username, String f, String l, String pw, Course course){
super(username, f, l, pw);
this.course = course;
}
public Student(){
super(String username, String f, String l, String pw);
}
//Getter and Setter Methods
}
现在我的麻烦开始了,在我的第 3 个 class,名为 Window 中,我能够向我的 ArrayList 添加一个新学生。为了进行调试,我在控制台中打印了该列表的大小,它比以前更大,所以我确定我的数组列表中有一个新学生。
这个window有点像注册过程。注册成功后用户可以关闭window。如果他在注册后立即尝试登录,那是行不通的,因为在我关闭(注册)window:
后,我的数组列表再次为空
public class Window{
Student student 0 = new Student();
private void saveButtonActionPerformed(ActionEvent evt) {
String first;
String last;
String pw;
String username;
Course kurse1;
if(condition){
//get userinputs
first = fName.getText();
last = lName.getText();
pw = password.getText();
username = first.toLowerCase() + "." + last.toLowerCase();
kurse1 = new Course(kurseN1.getText());
//add Student to list
Student student = new Studenten(username, first, last, pw, new Kurse(kurseN1.getText()));
student0.studentens.add(student);
System.out.println("Number of students: " + student0.students.size());
}else{
JOptionPane.showMessageDialog(null, "please fill out all required fields");
}
}
//with this method I open the login window
private void btnClick(ActionEvent e){
LoginWindow login = new LoginWindow();
login.setVisible(true);
}
您混淆了 public 和静态。 arrayList 是 User 的(非静态)成员,因此会为您创建的每个 User 创建一个新的 ArrayList。
如果它是一个静态变量,这意味着在 JVM 中只会创建一个列表,并且 User 的所有实例都共享该列表。我认为这就是您的意图。
Public只是指属于其他类的代码是否可以直接看到和使用它,并不影响它的创建方式、时间或频率。
旁注:ArrayList 不是线程安全的。因此,如果您通过两个不同的线程访问它,例如您经常进入 Web 应用程序甚至现代 UI,您可能会遇到一些意想不到的冲突。如果两个人试图写入它,一个写入可能会丢失,或者整个东西可能会损坏。
我有不同的 class,但只有一个 public ArrayList,每个 class 都可以访问它。我的第一个 class 叫做 User,看起来像这样
public class User{
private String fName, lName, username, password;
public ArrayList<Student> students = new ArrayList<>();
public User(String username, String fName, String lName, String password){
this.username = username;
this. fName = fName;
this.lName = lName;
this.password = password
}
//Getter and Setter Methods here
//here my own methods
public void addStudent(Student student){
students.add(student);
}
}
我的第二个 class 叫做 Student,看起来像这样
public class Student extends User{
private String username;
private String fName;
private String lName;
private String password;
private Course course;
public Student(String username, String f, String l, String pw, Course course){
super(username, f, l, pw);
this.course = course;
}
public Student(){
super(String username, String f, String l, String pw);
}
//Getter and Setter Methods
}
现在我的麻烦开始了,在我的第 3 个 class,名为 Window 中,我能够向我的 ArrayList 添加一个新学生。为了进行调试,我在控制台中打印了该列表的大小,它比以前更大,所以我确定我的数组列表中有一个新学生。
这个window有点像注册过程。注册成功后用户可以关闭window。如果他在注册后立即尝试登录,那是行不通的,因为在我关闭(注册)window:
后,我的数组列表再次为空public class Window{
Student student 0 = new Student();
private void saveButtonActionPerformed(ActionEvent evt) {
String first;
String last;
String pw;
String username;
Course kurse1;
if(condition){
//get userinputs
first = fName.getText();
last = lName.getText();
pw = password.getText();
username = first.toLowerCase() + "." + last.toLowerCase();
kurse1 = new Course(kurseN1.getText());
//add Student to list
Student student = new Studenten(username, first, last, pw, new Kurse(kurseN1.getText()));
student0.studentens.add(student);
System.out.println("Number of students: " + student0.students.size());
}else{
JOptionPane.showMessageDialog(null, "please fill out all required fields");
}
}
//with this method I open the login window
private void btnClick(ActionEvent e){
LoginWindow login = new LoginWindow();
login.setVisible(true);
}
您混淆了 public 和静态。 arrayList 是 User 的(非静态)成员,因此会为您创建的每个 User 创建一个新的 ArrayList。
如果它是一个静态变量,这意味着在 JVM 中只会创建一个列表,并且 User 的所有实例都共享该列表。我认为这就是您的意图。
Public只是指属于其他类的代码是否可以直接看到和使用它,并不影响它的创建方式、时间或频率。
旁注:ArrayList 不是线程安全的。因此,如果您通过两个不同的线程访问它,例如您经常进入 Web 应用程序甚至现代 UI,您可能会遇到一些意想不到的冲突。如果两个人试图写入它,一个写入可能会丢失,或者整个东西可能会损坏。