简体   繁体   English

从arrayList中删除对象并打印出剩余的对象

[英]removing objects from an arrayList and printing out the remain objects

i have a code that stores the number of books in arraylist, every time a book is borrowed the user is prompted to input a number starting from zero which represents the first book and 1 for the second and so on. 我有一个代码,可以在arraylist中存储书籍的数量,每当借用一本书时,都会提示用户输入从零开始的数字,该数字代表第一本书,第二本书则是1,依此类推。 it runs at first deleting the first object from the arraylist, then when it gets to the last, it throws an IndexOutOfBoundException, here's my block of code, please help! 它首先运行,从arraylist中删除第一个对象,然后到达最后一个对象,它抛出IndexOutOfBoundException,这是我的代码块,请帮忙!

  //class doesn't do much than just gets the title of the book
 public class BookLibrary {
  String title;

 public BookLibrary(){}

public void setTitle( String names) {
    title = names;
}

public String getTitle(){
    return title;
}
}

here's my code that deletes the object from the arraylist public class LibraryAssistant { 这是我的代码,该代码从arraylist公共类LibraryAssistant {

 ArrayList<BookLibrary> booklib = new ArrayList<>();
 int numOfBooks = 0;

 public void setupLibrary(){
    BookLibrary bc = new BookLibrary();
    bc.setTitle("fantastic beasts and where to find them");
    BookLibrary bc1 = new BookLibrary();
    bc1.setTitle("Harry potter and the prisoner of azkabans");
    BookLibrary bc2 = new BookLibrary();
    bc2.setTitle("one day for the thief");
    booklib.add(bc);
    booklib.add(bc1);
    booklib.add(bc2);
    numOfBooks++;

    for(BookLibrary book : booklib){
       System.out.println(book.getTitle());

    }
}

public void borrowbook(){
    while(!booklib.isEmpty()){
        String getbooknum = userInput("please enter a book number of the             book you want to borrow");
        int index = Integer.parseInt(getbooknum);
        if(index>=0){
            booklib.remove(index);
            for(BookLibrary lb : booklib){
                System.out.println(lb.getTitle());
            }
        }
        numOfBooks--;
    } if(booklib.isEmpty()){
        System.out.println("the library is empty");
    }
}

The reason behind the IndexOutOfBoundException is the size of arraylist gets decremented everytime you remove the object. IndexOutOfBoundException背后的原因是,每次删除对象时arraylist的大小都会减小。 so if you remove the first title (index 0) from the book the size of list will be 2. when user go for second removal (index 1), it will work fine. 因此,如果您从书中删除第一个标题(索引0),则列表的大小将为2。当用户进行第二次删除(索引1)时,它将正常工作。 But when user will go for 3rd removal (index 2), it will throw IndexOutOfBoundException as the size of ArrayList is 1 now. 但是,当用户要进行第三次移除(索引2)时,由于ArrayList的大小现在为1,它将抛出IndexOutOfBoundException。

My first observation is that numOfBooks in setUpLibrary() should be set to 3. or should be updated as the size of the array list. 我的第一个观察结果是setUpLibrary()中的numOfBooks应该设置为3,或者应该更新为数组列表的大小。 The second observation is that in borrowbook(), while loop runs until the bookLib becomes empty. 第二个观察结果是在borrowbook()中,while循环运行直到bookLib为空。 So line booklib.remove(index); 因此,行booklib.remove(index); will throw error IndexOutOfBoundsException when there are no more elements or the number given by the user is out of range. 当没有更多元素或用户给定的数字超出范围时,将抛出ErrorIndexOutOfBoundsException。

Please refer below documentation: https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#remove(int) 请参考以下文档: https : //docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#remove(int)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM