我想从ArrayList中删除所有项目。当我从主类调用deletBook()方法时,会出现ArrayList范围问题,并且我无法从列表中删除总项目。 addBook()和searchBook方法在deletBook()中成功运行问题。
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);
}
}
答案 0 :(得分:0)
为了获得更好的可重新编码代码,您可以创建一个BookInfoNews对象并在其他类中创建方法,然后只需完成您的工作即可完成。
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();
}
}