java无法将对象添加到ArrayList中
如果我创建了名为entires的arraylist,为什么没有定义entires变量
public class College
{
Student s = new Student();
ArrayList <Student> entries;
public College()
{
entries = new ArrayList<Student>();
}
public void add(Student s)
{
entires.add(s);
}
}
是否已编辑,以便类可以使用ArrayList
# 1 楼答案
实际上,您必须在
class
范围内定义ArrayList
,如这样,所有的类都可以访问
ArrayList
# 2 楼答案
# 3 楼答案
entries变量是在构造函数中本地创建的。无法在您的方法中访问它。让你的作品全球化
# 4 楼答案
entries变量是在本地定义的。定义如下:
# 5 楼答案
您应该将
entries
变量定义为类成员,以便在类方法中访问该变量应该是这样的: