Java ArrayList 的对象的 add() 方法不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19527687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java ArrayList of objects' add() method not working
提问by InamTaj
Im new to JAVA, and Im facing a beginer's problem, I know :-P
我是 JAVA 新手,我面临初学者的问题,我知道:-P
IN PLAIN ENGLISH => I'm trying to create an ArrayList of objects, and to add new objects in the array when required.
IN PLAIN ENGLISH => 我正在尝试创建一个对象的 ArrayList,并在需要时在数组中添加新对象。
OK, so here is a compact version of my code
好的,这是我的代码的紧凑版本
package ACP.Employee; //created package
import java.util.ArrayList; //imported arraylist class
import ACP.Employee.EmployeeClass; //imported employee class of same package
public class ClientClass
{
ArrayList<EmployeeClass> empArray = new ArrayList<EmployeeClass>(); //created a new Array List
public static void main (String[] args)
{
int objcount = 0; //variable to store objct count
empArray.add(objcount, EmployeeClass obj);
}
}
The IDE (Eclipse) gives me following errors on empArray.addline
IDE (Eclipse) 在empArray.add行上给我以下错误
- Cannot make a static reference to the non-static empArray
- EmployeeClass cannot be resolved to a variable
- Syntax error on token obj, delete this token.
- 无法对非静态 empArray 进行静态引用
- EmployeeClass 无法解析为变量
- 标记 obj 上的语法错误,删除此标记。
I have also tried to replace that line with the following syntax,
我还尝试用以下语法替换该行,
empArray.add(new EmployeeClass());
BUTthe following error remains.
但以下错误仍然存在。
- Cannot make a static reference to the non-static empArray
- 无法对非静态 empArray 进行静态引用
Kindly help out here, I have seen API Spec of ArrayList's add() method, which is following:::
请在这里提供帮助,我已经看到了 ArrayList 的 add() 方法的 API Spec,如下所示:::
void add(int index, Object element) ==>> Inserts the specified element at the specified position index in the list boolean add(Object o) ==>> Appends the specified element to the end of this list. SOURCE (http://www.tutorialspoint.com/java/java_arraylist_class.htm)
void add(int index, Object element) ==>> 在列表中的指定位置 index 插入指定元素 boolean add(Object o) ==>> 将指定元素附加到此列表的末尾。来源(http://www.tutorialspoint.com/java/java_arraylist_class.htm)
采纳答案by arynaq
There are two problems with your code.
您的代码有两个问题。
You are trying to access a member of the class
ClientClass
from a static context (main). This is only possible if that member is static. SinceempArray
has no modifiersit will default to package-private which is not static. You will either have to make it accessible in a static context by declaring it static:static ArrayList<EmployeeClass> empArray = new ArrayList<>();
or create an instance of ClientClass and access its memberArrayList<EmployeeClass> empArray = new ArrayList<EmployeeClass>(); public static void main(String[] args) { ClientClass t = new ClientClass(); t.empArray.add(new Employee()); }
The way you are passing employees to the list to be added will not compile in java. Here is a good tutorialon how to create objects in java. In fact it seems you are new to Java, so I recommend you start on page one of the tutorials, they are very good for getting familiar with the language fast and you will be productive in no time.
您正在尝试
ClientClass
从静态上下文 (main)访问类的成员。这只有在该成员是静态的情况下才有可能。由于empArray
没有修饰符,它将默认为非静态的 package-private。您必须通过将其声明为静态来使其在静态上下文中可访问:static ArrayList<EmployeeClass> empArray = new ArrayList<>();
或者创建一个 ClientClass 的实例并访问它的成员ArrayList<EmployeeClass> empArray = new ArrayList<EmployeeClass>(); public static void main(String[] args) { ClientClass t = new ClientClass(); t.empArray.add(new Employee()); }
您将员工传递给要添加的列表的方式不会在 java 中编译。这是一个关于如何在java中创建对象的好教程。事实上,您似乎是 Java 的新手,所以我建议您从教程的第一页开始,它们非常有助于快速熟悉该语言,而且您很快就会提高工作效率。
回答by Ilya
From static
method you have access only to static
fields.
From not-static
method you have access to all fields.
E.g
从static
方法您只能访问static
字段。
从not-static
方法中,您可以访问所有字段。
例如
private int i = 0;
private static int j = 0;
public void increment()
{
i++; // correct
j++; // correct
}
public static void staticIncrement()
{
i++; // compilation error
j++; // correct
}
So, in you example empArray
should be static
.
所以,在你的例子中empArray
应该是static
.
public class ClientClass
{
static ArrayList<EmployeeClass> empArray = new ArrayList<EmployeeClass>();
public static void main (String[] args)
{
int objcount = 0; //variable to store objct count
empArray.add(objcount, new EmployeeClass());
empArray.add(new EmployeeClass());
}
}