我想將數據添加到ArrayList對象。在我的代碼中,addBook()方法將顯示Input Dialogue Box並將此字符串傳遞給isbn變量。現在需要將isbn變量數據添加到駐留在BookInfoNew()構造函數中但不在addBook()方法中找到的列表對象的ArrayList中。 (list.add(isbn))list.add()不會將數據添加到ArrayList
請幫助我。
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;
//default constructor
public BookInfoNew() {
List<String> list = new ArrayList<String>(); //create ArrayList
}
//Parameterized constructor
public void BookInfoNew(String x, String y, String z) {
isbn = x;
bookName = y;
authorName = z;
}
//add book method
public void addBook() {
String isbn = JOptionPane.showInputDialog("Enter ISBN");
//add books data to ArrayList
list.add(isbn);
}
}
因爲'list'是一個局部變量,它只存在於你的非參數構造函數中。 – SomeJavaGuy
爲什麼其他字段是靜態的? – Manu
你的問題標題是錯誤的,因爲你的'addBook()'甚至不能訪問'list' – Ramanlfc