ArrayList不能存储对象

3

你好,我遇到了ArrayList中打印输出项目的问题。在我的PatronBorrow方法中可以打印输出,但是在PatronList和PatronReturn方法中没有打印任何东西。有人能告诉我代码哪里出了问题吗?非常感谢大家。

package proj1;

import java.util.ArrayList;
import java.util.List;


public class Patron {

private int id;
private Book book;
private List<Book> books;

public Patron(int id){
    this.id = id;
    books = new ArrayList<Book>();
}

public int getID(){
    return id;
}

public List<Book> getBooks(){
    return books;
}

public void PatronBorrow(String b){
    book = new Book(b);
    books.add(book);
    System.out.println("Patron " + id + " has borrowed " + book.getTitle());
}

public void PatronReturn(String b){
    for(Book book : books){
        if(book.getTitle().equals(b)){
            books.remove(book);
            System.out.println("Patron " + id + " has borrowed " +     book.getTitle());
        }
    }
}

public void PatronList(){
    for(Book b : books){
        System.out.println("Patron " + id + " has borrowed " + books.size() + " item(s)");
        System.out.println(b);
    }
}

}

package proj1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Project1 {

public static boolean isNumeric(String str){
    for(char c : str.toCharArray()){
        if(Character.isDigit(c)){
            return true;
        }
    }
    return false;
}

public static void main(String[] args){

    String command;
    String line;
    Patron patron;
    int patronID;
    String title;
    String newTitle;
    String infile = args[0];

    if (args.length != 1){
        throw new IllegalArgumentException("Enter in file name");
    }

    try{
        Scanner file = new Scanner(new FileInputStream(infile));
        while(file.hasNext()){
            command = file.next();
            if(isNumeric(command)){
                patronID = Integer.parseInt(command);
                patron = new Patron(patronID);
                command = file.next();
                if(command.equals("borrow")){
                    title = file.nextLine();
                    newTitle = title.substring(2, title.length() - 1);
                    patron.PatronBorrow(newTitle);
                }else if(command.equals("return")){
                    title = file.nextLine();
                    newTitle = title.substring(2, title.length() - 1);
                    patron.PatronReturn(newTitle);
                }else if(command.equals("list")){
                    patron.PatronList();
                }
            }else{

            }
        }

    }catch (FileNotFoundException e) {
        System.out.println("File not found" + e.getMessage());
        System.exit(0);
    }


}

}


我不明白为什么在 PatronList 中没有打印任何内容。你确定没有重新初始化 Patron 对象或者在添加书籍对象之前调用了方法吗? - Avinash Singh
根据您调用这些方法的方式,类中的最后两个方法绑定在空列表上进行迭代?此外,请注意您在PatronReturn中删除了唯一添加的书籍。您想要实现什么目的? - user626607
你能否将你的主方法添加到问题中,这样我们就可以看到调用这个类的代码。 - Jyro117
当您尝试在PatronReturn和PatronList方法中打印它时会发生什么?实际上,books arraylist 中是否有要打印的书籍? - knoight
1
让我们看一下调用这个代码的代码。有几个小问题,但它们不应该阻止任何内容被打印——实际上,它们应该导致额外/错误的文本被打印,或者引发异常。 - cHao
你能给我 book 类吗? - William Kinaan
1个回答

4
在使用Patron类的循环中,每次都创建一个新的(空白)Patron
如果你想在不同的读者之间切换,你需要在主函数中创建一个Map<Integer, Patron> patrons,每次改为从patrons中检索,并且只有在其中没有读者时才创建一个(并将其存储在map中)。
(关于你的Patron类,当你删除一本书时,你可能会发现这个类经常会抛出PatronReturn异常。具体来说,因为你正在遍历的列表上进行删除操作,导致ConcurrentModificationException。请注意这一点。)

哦,我现在明白了,我的文件第二行有另一个ID,所以它创建了一个新的......谢谢!还有,map和list有什么区别? - user2127726
@user2127726:地图是一种查找表。使用Map<Integer, Patron>,您可以以这样的方式存储Patron,即可以通过整数ID查找它。 - cHao
为什么要使用 Map 而不是 List,这其实取决于您的客户ID的性质。如果数字之间会有间隔或者它们的长度超过几位数,那么使用 List 就不太好了,因为您将浪费一大堆可能永远都没有值的列表插槽空间。如果查找空间是稀疏的,Map 更适合处理。如果 ID 总是像 1、2、3 这样的话,那么 List 也可以使用,但这严重依赖于数据;代码本身并不能让我做出这个假设。 - cHao

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接