我正在尝试编写一个实现arraylist的类,但是每当元素添加到列表或从列表中删除时,我都会观察并重绘gui。语法上一切都正确,但是我的代码中似乎存在逻辑错误,因为我遇到了stackoverflowerror,这使我相信我缺少某个地方的递归调用。确切的错误是Exception in thread "main" java.lang.StackOverflowError at java.util.AbstractList.<init>(Unknown Source) at java.util.ArrayList.<init>(Unknown Source) at java.util.ArrayList.<init>(Unknown Source) at Library.<init>(Library.java:45) at LoanList.<init>(LoanList.java:14) at Library.<init>(Library.java:47) at LoanList.<init>(LoanList.java:14)
实际的代码大约在1k(1,000)行左右,所以我只发布我认为应该归咎的位。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Library implements Serializable, LibraryInterface, ActionListener{
private static final long serialVersionUID = 1L;
private List<Student> students;
private List<Book> books;
private LoanList<Loan> loans;
private JButton ok, cancel;
private JTextField utxt,ntxt,etxt;
private JTextArea list;
private JFrame frame;
public Library(){
students = new ArrayList<Student>();
books = new ArrayList<Book>();
loans = new LoanList<Loan>();
}
/* (non-Javadoc)
* @see LibraryInterface#loadBooks()
*/
/* (non-Javadoc)
* @see LibraryInterface#loadBooks()
*/
@Override
public void loadBooks() throws ClassNotFoundException, IOException{
FileInputStream fis = new FileInputStream("Books.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
books = (ArrayList<Book>) ois.readObject();
ois.close();
}
/* (non-Javadoc)
* @see LibraryInterface#loadStudents()
*/
/* (non-Javadoc)
* @see LibraryInterface#loadStudents()
*/
@Override
public void loadStudents() throws ClassNotFoundException, IOException{
FileInputStream fis = new FileInputStream("Students.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
students = (ArrayList<Student>) ois.readObject();
ois.close();
}
/* (non-Javadoc)
* @see LibraryInterface#loadLoans()
*/
/* (non-Javadoc)
* @see LibraryInterface#loadLoans()
*/
@Override
public void loadLoans() throws ClassNotFoundException, IOException{
FileInputStream fis = new FileInputStream("Loans.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
loans = (LoanList<Loan>) ois.readObject();
ois.close();
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class LoanList<Loan> extends Library implements List<Loan>{
/**
*
*/
private static final long serialVersionUID = 1L;
private ArrayList<Loan> list;
public LoanList(){
list = new ArrayList<Loan>();
}//Rest of the arraylist methods follow, however it's not constructing so no need to post
import java.io.Serializable;
public class Loan implements Serializable{
private String uid;
private String bookId;
private String date;
public Loan(String uid,String bookid,String date){
this.uid = uid;
this.bookId = bookid;
this.date = date;
}
public String getUid() {
return uid;
}
public String getBookId() {
return bookId;
}
public String getDate() {
return date;
}
public String toString(){
String x="";
x = "BookID: " + this.bookId + "\n" + "Student UID: " + this.uid + "\n" + "Date Checkedout: " + this.date + "\n" + "\n";
return x;
}
编辑新的重绘方法
public void framePaint(){
Collections.sort(students,new studentComparator());
String result="";
for(int i=0;i<students.size();i++){
result += "\n" + students.get(i).toString() + "\n";
for(int j=0;j<loans.size();j++){
if(loans.get(j).getUid().equals(students.get(i).getUid())){
result += "Book ID: " + loans.get(j).getBookId() + "\n";
}
}
}
list.setText(result);
frame.repaint();
比较器供参考
public class studentComparator implements Comparator<Student>{
@Override
public int compare(Student arg0, Student arg1) {
return arg0.getUid().compareTo(arg1.getUid());
}
}
我正在尝试重新绘制框架的GUI
public void listStudents(){
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel,1));
list = new JTextArea("haha");
list.setEditable(false);
list.setBackground(Color.YELLOW);
list.setForeground(Color.BLACK);
Collections.sort(students,new studentComparator());
String result="";
for(int i=0;i<students.size();i++){
result += "\n" + students.get(i).toString() + "\n";
for(int j=0;j<loans.size();j++){
if(loans.get(j).getUid().equals(students.get(i).getUid())){
result += "Book ID: " + loans.get(j).getBookId() + "\n";
}
}
}
list.setText(result);
panel.add(list);
JScrollPane scroll = new JScrollPane(panel);
frame.add(scroll);
frame.setResizable(true);
frame.setSize(300, 300);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(dim.width/2-frame.getSize().width/2, dim.height/2-frame.getSize().height/2);
frame.setVisible(true);
最佳答案
LoanList正在扩展Library。库的构造函数创建一个新的LoanList。这个新的LoanList是一个库,因此它创建了一个新的LoanList。这个新的LoanList是一个库,因此它创建了一个新的LoanList。这个新的LoanList是一个库,因此它创建了一个新的LoanList。广告无限。