首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >调用deleteBook()方法从ArrayList中删除所有项

调用deleteBook()方法从ArrayList中删除所有项
EN

Stack Overflow用户
提问于 2015-11-19 09:47:45
回答 1查看 1.1K关注 0票数 0

我想从ArrayList中删除所有的项目。当我从主类调用deletBook()方法时,会出现ArrayList作用域问题,并且无法从列表中删除总项。addBook()和searchBook方法成功地运行了deletBook()中的问题。

代码语言:javascript
运行
复制
import java.util.*;
import javax.swing.JOptionPane;

public class BookInfoNew {
    private static String isbn;
    private static String bookName;
    private static String authorName;
    public static int totalBooks = 0;
    public static List<String> list;

    //default constructor
    public BookInfoNew() {
        list = new ArrayList<String>(); //create ArrayLis
    }
    //add book method
    public void addBook() {

        //show Input dialogue box to take input
        String isbn = JOptionPane.showInputDialog("Enter ISBN");
        String bookName = JOptionPane.showInputDialog("Enter Book name");
        String authorName = JOptionPane.showInputDialog("Enter Author name");

        //add books data to ArrayList
        list.add(isbn);
        list.add(bookName);
        list.add(authorName);
        totalBooks++; //increment Total books

        //show notification after successful added book
        JOptionPane.showMessageDialog(null, "Book Name:  " +bookName + "  added successful\n" + "Total Books: " +totalBooks);

    }
    //delete book method
    public void deletBook(Collection c) {
        String del = JOptionPane.showInputDialog("Enter book name to delete"); //show input dialogue

        Iterator<String> iter = c.iterator(); //going from one item to next in list

        //remove items from list
        while(iter.hasNext()) {
            iter.next();
            list.remove(del);
        }
        totalBooks--; //decrement total books
    }
}

//Main class
import java.util.*;
import javax.swing.JOptionPane;;

public class BookInfoNewMain {
    public static void main(String[] args) {
        BookInfoNew obj = new BookInfoNew();

        obj.addBook();
        obj.searchBook();
obj.deletBook(list);
    }
}
EN

Stack Overflow用户

回答已采纳

发布于 2015-11-19 10:39:03

为了获得更好的可阅读代码,您可以创建一个BookInfoNews对象并在其他类中创建方法,然后只需完成您的操作就可以了。

代码语言:javascript
运行
复制
package test;
import java.util.*;
import javax.swing.JOptionPane;

public class BookInfoMethod {
public static int totalBooks = 0;
public static List<BookInfoNews> list = new ArrayList<BookInfoNews>();      //create ArrayLis;

//default constructor
public BookInfoMethod() {
}
//add book method
public void addBook() {

    //show Input dialogue box to take input
    String isbn = "test";
    String bookName = "test";
    String authorName = "test";

    //add books data to ArrayList
    BookInfoNews newBook = new BookInfoNews(isbn, bookName, authorName);
    list.add(newBook);
    totalBooks++; //increment Total books

    //show notification after successful added book
    JOptionPane.showMessageDialog(null, "Book Name:  " +bookName + "  added successful\n" + "Total Books: " +totalBooks);

}
//delete book method
public void deletBook() {
    String del = "test";

   for (int i = 0; i < this.list.size() ; i++){
       if(del.equals(this.list.get(i).getBookName())){
           this.list.remove(i);
       }
   }
    totalBooks--; //decrement total books
    JOptionPane.showMessageDialog(null,"Total Books: " +totalBooks);
}

public BookInfoNews searchMethod(String bookName){
if(bookName == null){
    return null;
}
for(BookInfoNews tmp : this.list){
    if(bookName.equals(tmp.getBookName()))
        return tmp;
}
return null;
}
}

class BookInfoNews{
    private String isbn;
    private String bookName;
    private String authorName;
//default constructor
public BookInfoNews(String isbn,String bookName,String authorName) {
    this.isbn = isbn;
    this.bookName = bookName;
    this.authorName = authorName;
}
public String getIsbn() {
    return isbn;
}
public void setIsbn(String isbn) {
    this.isbn = isbn;
}
public String getBookName() {
    return bookName;
}
public void setBookName(String bookName) {
    this.bookName = bookName;
}
public String getAuthorName() {
    return authorName;
}
public void setAuthorName(String authorName) {
    this.authorName = authorName;
}


}

class BookInfoNewMain {
public static void main(String[] args) {
    BookInfoMethod obj = new BookInfoMethod();
    obj.addBook();
    obj.deletBook();
}
}
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33800287

复制
相关文章

相似问题

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