Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“线程中的异常”;“主要”;java.lang.StackOverflowerr(创建断点)_Java_Linked List - Fatal编程技术网

“线程中的异常”;“主要”;java.lang.StackOverflowerr(创建断点)

“线程中的异常”;“主要”;java.lang.StackOverflowerr(创建断点),java,linked-list,Java,Linked List,这是一个简单的图书馆代码,其中图书被添加到图书馆列表中。查找返回布尔值的方法。借阅方法,从libraryList中删除图书并将图书移动到借阅列表。return方法从借阅列表中删除图书,并将图书移回libraryList。我使用的是IntelliJ,代码在默认包下。请帮我找出问题并解决它。多谢各位 LibraryDriver.java import java.util.Scanner; public class LibraryDriver { LibraryList library =

这是一个简单的图书馆代码,其中图书被添加到图书馆列表中。查找返回布尔值的方法。借阅方法,从libraryList中删除图书并将图书移动到借阅列表。return方法从借阅列表中删除图书,并将图书移回libraryList。我使用的是IntelliJ,代码在默认包下。请帮我找出问题并解决它。多谢各位

LibraryDriver.java

import java.util.Scanner;
public class LibraryDriver 
{
    LibraryList library = new LibraryList();
    BorrowedList borrowed = new BorrowedList();
    Book b;

    public void addBook(Scanner in){
        System.out.println("Enter Books's ISBN: ");
        int isbn = in.nextInt();
        System.out.println("Enter Book's Title: ");
        String title = in.next();
        System.out.println("Enter Book's Author Name: ");
        String author = in.next();
        b = new Book (isbn, title, author);
        library.addBook(b);
    }

    public void findBook(Scanner in){
        System.out.println("Enter the Books ISBN to search: ");
        int isbn = in.nextInt();
        if(library.findBook(isbn) == true)
        {
            System.out.println("Book is available in Library");
        }
        else if(borrowed.findBook(isbn) == true)
        {
            System.out.println("Book is borrowed by a Student");
        }
        else
            System.out.println("Sorry, Book is not available");
    }

    public void borrowBook(Scanner in){
        System.out.println("Enter the ISBN of Book you want to borrow: ");
        int isbn = in.nextInt();
        if(library.findBook(isbn) == true){
            library.loanBook(isbn);
        }
    }

    public void returnBorrowedBook(Scanner in){
        System.out.println("Enter the ISBN of Book you want to return: ");
        int isbn = in.nextInt();
        if(borrowed.findBook(isbn) == true){
            borrowed.returnBook(isbn);
        }
    }

    public void printAllBooks(){
        library.printBook();
        borrowed.printBook();
    }

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        LibraryDriver driver = new LibraryDriver();
        System.out.println("Welcome to Murtaza's Library");
        System.out.println("--------------------------------");
        boolean done = false;
        while (!done)
        {
            System.out.print("Please select an action: (F)ind, (A)dd, (B)orrowed, (P)rint, (R)eturn or (Q)uit: ");
            String choice = in.next();
            if (choice.equals("F") || choice.equals("f")){
                driver.findBook(new Scanner(System.in));
            }
            else if (choice.equals("A") || choice.equals("a")){
                driver.addBook(new Scanner(System.in));
            }
            else if (choice.equals("B") || choice.equals("b")){
                driver.borrowBook(new Scanner(System.in));
            }
            else if (choice.equals("R") || choice.equals("r")){
                driver.returnBorrowedBook(new Scanner(System.in));
            }
            else if (choice.equals("P") || choice.equals("p")){
                driver.printAllBooks();
            }
            done = choice.equals("Q") || choice.equals("q");
        }
        System.out.println("Good bye.Have a nice day!");
        in.close();
    }
}
LibraryList.java

import java.util.LinkedList;

public class LibraryList {
    static private LinkedList<Book>libraryList;
    BorrowedList bList = new BorrowedList();

    public LibraryList() {
        libraryList = new LinkedList<Book>();
    }

    public void addBook(Book b) {
        libraryList.add(b);
    }

    public void loanBook(int isbn){
        for(int i = 0; i < libraryList.size() ;i++){
            if(isbn == libraryList.get(i).getIsbn()){
                bList.addBook(libraryList.get(i));
                libraryList.remove(i);
                System.out.println("Request has been processed. Collect");
            }
        }
    }

    public boolean findBook(int isbn){
        boolean value = false;
        for(int i = 0; i < libraryList.size();i++){
            if(libraryList.get(i).getIsbn() == isbn){
                value= true;
                break;
            }
            else
                value= false;
        }
        return value;
    }
    public void printBook() {
        System.out.println("==================================");
        System.out.println("All Books in Library are: ");
        System.out.println("==================================");
        for(int i = 0; i < libraryList.size();i++){
            System.out.println(libraryList.get(i));
        }
    }
}
错误

线程“main”java.lang.StackOverflower中出现异常 在BorrowedList。(BorrowedList.java:6) 在LibraryList.(LibraryList.java:6) 在BorrowedList。(BorrowedList.java:6) 在LibraryList.(LibraryList.java:6) 在BorrowedList。(BorrowedList.java:6) 在LibraryList.(LibraryList.java:6) 在BorrowedList。(BorrowedList.java:6) 在LibraryList.(LibraryList.java:6) 不断重复。
由于这一行的原因,每次实例化借阅列表时都会实例化一个LibraryList
LibraryList lib=new LibraryList()

另一方面,每次实例化LibraryList时,都会实例化一个借阅列表,因为这一行
借阅列表bList=new borroweedList()

这会导致实例化的无限循环,从而导致堆栈溢出

相反,您应该将借用列表的实例作为LibraryList(或相反)的构造函数参数提供

公共类库列表{
私人借阅名单;
公共图书馆员(){
this.bList=新借用列表(this);
}
}
私有类借用列表{
私人图书馆主义自由党;
公共借阅列表(图书馆列表库){
this.lib=lib;
}
}

您创建的每个
库列表
都将创建一个
借阅列表
,而借阅列表又将创建一个
库列表
,因此您将陷入对象创建的无限循环中。你需要打破这个圈子,问问自己,例如,创建一个
借用列表
是否真的需要该
借用列表
来创建一个全新的
库列表
。欢迎使用堆栈溢出@谢谢你。你的代码运行正常,我只需要在LibraryDriver中传递一个参数。我唯一的困惑是,在LibraryList构造函数中作为参数传递的“this”代表什么?
import java.util.LinkedList;

public class BorrowedList {
    static private LinkedList<Book>borrowedList;
    LibraryList lib = new LibraryList();

    public BorrowedList() {
        borrowedList = new LinkedList<Book>();
    }

    public void addBook(Book b) {
        borrowedList.add(b);
    }

    public void returnBook(int isbn){
        for(int i = 0; i < borrowedList.size() ;i++){
            if(isbn == borrowedList.get(i).getIsbn()){
                lib.addBook(borrowedList.get(i));
                borrowedList.remove(i);
                System.out.println("Request has been processed.");
            }
        }
    }

    public boolean findBook(int isbn){
        boolean value = false;
        for(int i = 0; i < borrowedList.size();i++){
            if(borrowedList.get(i).getIsbn() == isbn){
                value= true;
                break;
            }
            else
                value= false;
        }
        return value;
    }

    public void printBook() {
        System.out.println("==================================");
        System.out.println("All Books Borrowed by Students are: ");
        System.out.println("==================================");
        for(int i = 0; i < borrowedList.size();i++){
            System.out.println(borrowedList.get(i));
        }
    }
}
public class Book {
    private int isbn;
    private String title;
    private String author;

    public Book(int isbn, String title, String author) {
        this.isbn = isbn;
        this.title = title;
        this.author = author;
    }
    public int getIsbn() {
        return isbn;
    }

    public void setIsbn(int isbn) {
        this.isbn = isbn;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        String str = "";
        str += "ISBN: " + isbn + "\n";
        str += "Title: " + title + "\n";
        str += "Author: " + author + "\n";
        return str;
    }
}
Exception in thread "main" java.lang.StackOverflowError
    at BorrowedList.<init>(BorrowedList.java:6)
    at LibraryList.<init>(LibraryList.java:6)
    at BorrowedList.<init>(BorrowedList.java:6)
    at LibraryList.<init>(LibraryList.java:6)
    at BorrowedList.<init>(BorrowedList.java:6)
    at LibraryList.<init>(LibraryList.java:6)
    at BorrowedList.<init>(BorrowedList.java:6)
    at LibraryList.<init>(LibraryList.java:6)
    and keeps repeating.