簡體   English   中英

為什么我的 stack.get(stack.size-1) 在我的代碼中不起作用?

[英]Why is my stack.get(stack.size-1) not working in my code?

的廣告 class 中的返回 stack.get(stack.size-1).getBookName()不起作用。 這行代碼在 pop 中。 每當我運行它並想從堆棧的最頂部刪除某些內容時,它會刪除正確的內容,但不會返回刪除的正確書籍。 是錯誤的 .getBookName() 部分嗎? 請幫忙!

    //main class
import java.util.*;
class Main {
  public static void main(String[] args) {
  Scanner scan=new Scanner (System.in);
  Scanner scanString= new Scanner(System.in);
  int menuInput=0;
  String bookName;
  int pages;
  String author;

  ADT stack=new ADT ();

  while(menuInput!=6)
  {//menu
    System.out.println("1. Add an book to the stack");
    System.out.println("2. Remove an book from the stack");
    System.out.println("3. Return the top book");
    System.out.println("4. Return the length of the stack");
    System.out.println("5. Return if the list is empty");
    System.out.println("6. Exit");
    menuInput=scan.nextInt(); 
    if (menuInput==1)
  {
    System.out.println("What book do you want to add?");
    bookName=scanString.nextLine();
    System.out.println("How many pages are in the book?");
    pages=scan.nextInt();
    System.out.println("Who is the author of the book?");
    author=scanString.nextLine();
    stack.push(bookName, pages, author);
  }
    if (menuInput==2)
    {
      System.out.println(stack.pop());
    }
    if (menuInput==3)
    {
      System.out.println(stack.peek());
    }
    if (menuInput==4)
    {
      System.out.println(stack.length());
    }
    if (menuInput==5)
    {
      System.out.println(stack.isEmpty());
    }
  }

  }
}



    //adt class
import java.util.*;
public class ADT { //ADT stands for Abstract Data Type
String piece;
ArrayList <Book> stack = new ArrayList<Book>();
public ADT(){
  ArrayList <Book> stack = new ArrayList<Book>();
}
public ADT(Book b){
  ArrayList <Book> stack = new ArrayList<Book>();
  stack.add(b);
}
public void push(String bN,int pg, String aut)
{
Book one=new Book(bN,pg,aut);
stack.add(one);
}
public String pop()
{
stack.remove(stack.size()-1);
return "Book removed: " + stack.get(stack.size()-1).getBookName();
}
public String peek()
{
return "First book of the stack: " + stack.get(stack.size()-1).getBookName();
}
public int length()
{
return stack.size();
}
public boolean isEmpty()
{
  if (stack.size()==0)
  return true;
  else
  return false;
}
}



    //book class
public class Book{
  private String bookName;
  //name of the book
  private int pages;
  //pages in the book
  private String author;
  //author of the book
  public Book(String bN,int pg, String aut)//bN=book name, pg=pages, aut=author
  {
    bookName=bN;
    pages=pg;
    author=aut;
  }
  public String getBookName()
  {
    return bookName;
  }
}

您在remove() get() ) ,因此它會返回最后一本書。 如果您想要刪除的書,只需使用remove()的返回值:

public String pop() {
    return "Book removed: " + stack.remove(stack.size()-1).getBookName();
}

這不起作用的原因是您正在從堆棧中刪除 object,然后試圖在堆棧中找到它。 也就是說,您正確地調用了remove ,但隨后您在同一個堆棧上調用get ,如果有一個可用元素,它將返回下一個可用元素。

幸運的是, remove方法實際上返回了它刪除的 object。 所以你可以寫類似

public String pop()
{
    Book removedBook = stack.remove(stack.size()-1);
    return "Book removed: " + removedBook.getBookName();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM