Java list.add()不向ArrayList添加数据
我想向ArrayList对象添加数据。在我的代码中,addBook()方法将显示输入对话框,并将此字符串传递给isbn变量。现在需要将isbn变量数据添加到ArrayList,ArrayList位于BookInfoNew()构造函数中,但在addBook()方法中找不到list对象。(列表添加(isbn)) 请帮帮我Java list.add()不向ArrayList添加数据,java,arraylist,Java,Arraylist,我想向ArrayList对象添加数据。在我的代码中,addBook()方法将显示输入对话框,并将此字符串传递给isbn变量。现在需要将isbn变量数据添加到ArrayList,ArrayList位于BookInfoNew()构造函数中,但在addBook()方法中找不到list对象。(列表添加(isbn)) 请帮帮我 import java.util.*; import javax.swing.JOptionPane; public class BookInfoNew { privat
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);
}
}
import java.util.*;
导入javax.swing.JOptionPane;
公共类图书资讯新{
私有静态字符串isbn;
私有静态字符串bookName;
私有静态字符串authorName;
公共静态int totalBooks=0;
//默认构造函数
公共图书资讯新{
List List=新建ArrayList();//创建ArrayList
}
//参数化构造函数
public void BookInfoNew(字符串x、字符串y、字符串z){
isbn=x;
bookName=y;
authorName=z;
}
//加书法
公共无效地址簿(){
字符串isbn=JOptionPane.showInputDialog(“输入isbn”);
//将书籍数据添加到ArrayList
列表.添加(isbn);
}
}
这是一个范围问题。您无法访问addBook()
对象中的列表
对象。因此,您必须将list
设置为addBook()
的参数,或者将其设置为全局变量
此代码使用全局变量修复它:
import java.util.*;
import javax.swing.JOptionPane;
public class BookInfoNew {
private String isbn;
private String bookName;
private String authorName;
public int totalBooks = 0;
// global list variable here which you can use in your methods
private List<String> list;
//default constructor
public BookInfoNew() {
list = new ArrayList<String>(); //create ArrayList
}
//Parameterized constructor - constructor has no return type
public 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);
}
}
import java.util.*;
导入javax.swing.JOptionPane;
公共类图书资讯新{
专用字符串isbn;
私有字符串书名;
私有字符串authorName;
公共图书总量=0;
//全局列表变量,可以在方法中使用
私人名单;
//默认构造函数
公共图书资讯新{
list=新建ArrayList();//创建ArrayList
}
//参数化构造函数-构造函数没有返回类型
public BookInfoNew(字符串x、字符串y、字符串z){
isbn=x;
bookName=y;
authorName=z;
}
//加书法
公共无效地址簿(){
字符串isbn=JOptionPane.showInputDialog(“输入isbn”);
//将书籍数据添加到ArrayList
列表.添加(isbn);
}
}
您应该像这样重写代码:
...
List<String> list = null;
public BookInfoNew() {
list = new ArrayList<String>(); //create ArrayList
}
...
。。。
List=null;
公共图书资讯新{
list=新建ArrayList();//创建ArrayList
}
...
这应该没问题。因为
list
是一个局部变量,它只存在于非参数构造函数中。为什么其他字段是静态的
?您的问题标题是错误的,因为您的addBook()
甚至无法访问list
您的代码已经编辑过了。正如@manu所问的,为什么其他变量是静态的?现在list.add(isbn)不起作用。它说list.add(isbn)方法未定义。当list.add(isbn)将在BookInfoNew()构造函数中运行时,它工作得很好。如果您想在每次创建新的BookInfoNew
对象时运行addBook()
在构造函数中。也许您应该删除addBook()
方法中isbn
前面的字符串。这样,您将更新实例变量isbn
,而不是局部变量代码>