首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图书馆图书分类

图书馆图书分类
EN

Stack Overflow用户
提问于 2022-03-23 05:11:19
回答 1查看 986关注 0票数 1

我是一个java编程类,我不知道如何修复这个错误。

这就是我一直在犯的错误:

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

这就是我们的任务

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

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

例:如果输入是:

代码语言:javascript
复制
The Catcher in the Rye
9787543321724
J.D. Salinger

产出如下:

代码语言:javascript
复制
Number of linked list operations: 1
Number of ArrayList operations: 1

这是我的密码:

代码语言:javascript
复制
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();
       }
   }
}
EN

回答 1

Stack Overflow用户

发布于 2022-07-24 19:09:13

做过同一个实验室。它们使您对抛出的文件和文本数量感到恐惧,但要通过实验室,只需将这两行添加到Library.java的末尾:

代码语言:javascript
复制
  System.out.println("Number of linked list operations: " + linkedListLibrary.insertSorted(currNode, linkedListOperations));
  System.out.println("Number of ArrayList operations: " + arrayListLibrary.insertSorted(tempBook, arrayListOperations));

要解释一下:实验室是为了帮助您完成已建立的代码,并阅读它的集合方法,以学习如何在尚未创建的类中完成工作。如果您遵循开发存根(#TODO),您将跟踪找到需要做什么的路径。在这种情况下,这就是导致解决方案的原因!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71582097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档