【问题标题】:Library book sorting图书馆图书分拣
【发布时间】:2022-07-20 07:18:00
【问题描述】:

我正在学习 Java 编程课程,但我不知道如何解决此错误。

这是我不断收到的错误:

Library.java:120:错误:需要类、接口或枚举 导入 java.util.ArrayList; ^ 1 个错误

这是任务

已经创建了两个排序列表,一个使用链表 (LinkedListLibrary linkedListLibrary) 实现,另一个使用内置 ArrayList 类 (ArrayListLibrary arrayListLibrary) 实现。每个列表包含 100 本书(书名、ISBN 编号、作者),按 ISBN 编号升序排列。

通过使用各自的LinkedListLibraryArrayListLibrary insertSorted() 方法将新书插入每个列表并输出计算机插入新书必须执行的操作数来完成 main()。每个 insertSorted() 返回计算机执行的操作数。

例如:如果输入是:

The Catcher in the Rye
9787543321724
J.D. Salinger

输出是:

Number of linked list operations: 1
Number of ArrayList operations: 1

这是我的代码:

import java.util.Scanner;
import java.io.FileInputStream;
import java.io.IOException;
public class Library {
public static void fillLibraries(LinkedListLibrary linkedListLibrary, ArrayListLibrary arrayListLibrary) throws IOException {
   FileInputStream fileByteStream = null; // File input stream
   Scanner inFS = null; // Scanner object
   int linkedListOperations = 0;
   int arrayListOperations = 0;    
  
   BookNode currNode;
   Book tempBook;

   String bookTitle;
   String bookAuthor;
   long bookISBN;
  
   // Try to open file
   fileByteStream = new FileInputStream("Books.txt");
   inFS = new Scanner(fileByteStream);

   while (inFS.hasNextLine()) {
       bookTitle = inFS.nextLine();
       bookISBN = inFS.nextLong();
       inFS.nextLine();
       bookAuthor = inFS.nextLine();

       // Insert into linked list
       currNode = new BookNode(bookTitle, bookAuthor, bookISBN);
       linkedListOperations = linkedListLibrary.insertSorted(currNode, linkedListOperations);
       linkedListLibrary.lastNode = currNode;

       // Insert into ArrayList
       tempBook = new Book(bookTitle, bookAuthor, bookISBN);
       arrayListOperations = arrayListLibrary.insertSorted(tempBook, arrayListOperations);
   }
  
   fileByteStream.close(); // close() may throw IOException if fails
   }

   public static void main (String[] args) throws IOException {
       Scanner scnr = new Scanner(System.in);
       int linkedListOperations = 0;
       int arrayListOperations = 0;
  
       // Create libraries
       LinkedListLibrary linkedListLibrary = new LinkedListLibrary();
       ArrayListLibrary arrayListLibrary = new ArrayListLibrary();
  
       // Fill libraries with 100 books
       fillLibraries(linkedListLibrary, arrayListLibrary);
  
       // Create new book to insert into libraries
       BookNode currNode;
       Book tempBook;
  
       String bookTitle;
       String bookAuthor;
       long bookISBN;
  
       bookTitle = scnr.nextLine();
       bookISBN = scnr.nextLong();
       scnr.nextLine();
       bookAuthor = scnr.nextLine();

       // Insert into linked list
       currNode = new BookNode(bookTitle, bookAuthor, bookISBN);
       // TODO
       int i = linkedListLibrary.insertSorted(currNode,0);
  
       linkedListLibrary.lastNode = currNode;
  
       // Insert into ArrayList
       tempBook = new Book(bookTitle, bookAuthor, bookISBN);
       // TODO
       int j = arrayListLibrary.insertSorted(tempBook,0);
  
       // TODO: Print number of operations for linked list
       System.out.println("Number of operations for linked list : "+i);
       // TODO: Print number of operations for ArrayList
       System.out.println("Number of operations for ArrayList : "+j);
   }
}

// Book.java

public class Book{

   private String bookTitle;
   private String bookAuthor;
   private long bookISBN;

   public Book() {
       bookTitle = "";
       bookAuthor = "";
       bookISBN = 0;
   }

   public Book(String userBookTitle, String userBookAuthor, long userBookISBN) {
       bookTitle = userBookTitle;
       bookAuthor = userBookAuthor;
       bookISBN = userBookISBN;
   }
  
   public long getBookISBN() {
       return bookISBN;
   }
  
   public void printInfo(){
       System.out.println("Title: " + bookTitle);
       System.out.println("Author: " + bookAuthor);
       System.out.println("ISBN: " + bookISBN);
   }
}

// ArrayListLibrary.java

import java.util.ArrayList;
public class ArrayListLibrary {
   // ArraryList library
   public ArrayList<Book> library;

   public ArrayListLibrary() {
       library = new ArrayList<Book>();
   }
   public int insertSorted(Book newBook, int counter) {
       Book currBook;
       // Add an empty element at end of list
       library.add(null);
       // Loop through elements starting at the end
       for (int i = library.size() - 2; i >=0; --i) {
           currBook = library.get(i);

           // If the current book's ISBN is larger than newBook's ISBN, shift
           // the current book down 1, count shift operation
           if(currBook.getBookISBN() > newBook.getBookISBN()){
               library.set(i+1, currBook);
               ++counter;
           }
  
           // Otherwise, place newBook at the next location (empty slot),
           // count insert operation
           else {
               library.set(i+1, newBook);
               ++counter;
               return counter;
           }
       }
       // If we get to the top of the list, place newBook on top
       library.set(0, newBook);
       ++counter;
  
       return counter;
   }
  
   public void printLibrary() {
       for (int i = 0; i < library.size(); ++i) {
           library.get(i).printInfo();
           System.out.println();
       }
   }
}

// BookNode.java

public class BookNode {
   private String bookTitle;
   private String bookAuthor;
   private long bookISBN;
   private BookNode nextNodePtr; // Reference to the next node   
   public BookNode() {
       bookTitle = "";
       bookAuthor = "";
       bookISBN = 0;
       nextNodePtr = null;
   }
   // Constructor   
   public BookNode(String bookTitleInit, String bookAuthorInit, long bookISBNInit) {
       this.bookTitle = bookTitleInit;
       this.bookAuthor = bookAuthorInit;
       this.bookISBN = bookISBNInit;
       this.nextNodePtr = null;
   }
   // Constructor   
   public BookNode(String bookTitleInit, String bookAuthorInit, long bookISBNInit, BookNode nextLoc) {
       this.bookTitle = bookTitleInit;
       this.bookAuthor = bookAuthorInit;
       this.bookISBN = bookISBNInit;
       this.nextNodePtr = nextLoc;
   }
   // insertAfter
   public void insertAfter(BookNode nodeLoc) {
       BookNode tmpNext;

       tmpNext = this.nextNodePtr;
       this.nextNodePtr = nodeLoc;
       nodeLoc.nextNodePtr = tmpNext;
   }
   //setNext
   public void setNext(BookNode nodeLoc) {
       this.nextNodePtr = nodeLoc;
   }
   // Get location pointed by nextNodePtr   
   public BookNode getNext() {
       return this.nextNodePtr;
   }
   public long getBookISBN() {
       return this.bookISBN;
   }
   // TODO: Print book information
   public void printBookInfo() {
       System.out.println("Title: " + this.bookTitle);
       System.out.println("Author: " + this.bookAuthor);
       System.out.println("ISBN: " + this.bookISBN);
   }
}

// LinkedListLibrary.java

public class LinkedListLibrary {
   //Linked list nodes
   BookNode headNode;
   BookNode lastNode;

   LinkedListLibrary() {
       // Front of nodes list   
       headNode = new BookNode();
       lastNode = headNode;
   }

   public int insertSorted(BookNode newNode, int counter) {
       BookNode currNode, nextNode;
  
       // Special case for head node
       if (headNode == null || headNode.getBookISBN() >= newNode.getBookISBN()) {
           newNode.insertAfter(headNode);
           headNode = newNode;
       }
       else {
           // Locate the node before insertion point
           currNode = headNode;
  
           while (currNode.getNext() != null && currNode.getNext().getBookISBN() < newNode.getBookISBN()) {
               currNode = currNode.getNext();
           }
           newNode.setNext(currNode.getNext());
           currNode.insertAfter(newNode);
       }
  
       ++counter;
       return counter;
   }

   public void printLibrary() {
       BookNode currNode;

       currNode = headNode.getNext();
       while (currNode != null) {
           currNode.printBookInfo();
           System.out.println();
           currNode = currNode.getNext();
       }
   }
}

【问题讨论】:

  • 所有这些代码都在一个文件中吗?
  • 不是文件。它在 Zybooks 上的类似作业中。部分代码已包含在作业中,我们必须根据提示进行修改。

标签: java


【解决方案1】:

我在同一门课程中,他们突然将这个实验室突然出现在我们身上,他们从 0 到 100 的速度非常快。

你弄明白了吗?

【讨论】:

    猜你喜欢
    • 2016-02-05
    • 1970-01-01
    • 2015-05-27
    • 2011-11-03
    • 2016-02-24
    • 2012-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多