java 如何在静态方法中将其添加到数组列表中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4351064/
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
How do I add this to an arraylist within a static method?
提问by BB ON
I'm learning the ropes with Java and I've hit a snag with ArrayLists. The gist of my program is to take some user input parameters, create a class Foo with those parameters, and then add it to an arraylist. The problem is, it complains that I can't reference a non-static type from a static method. The only examples I can find online deal with adding constants ("Cat", "5.0" etc) to arraylists which doesn't really help me.
我正在学习 Java 的诀窍,但在使用 ArrayLists 时遇到了障碍。我的程序的要点是获取一些用户输入参数,使用这些参数创建一个类 Foo,然后将其添加到数组列表中。问题是,它抱怨我无法从静态方法中引用非静态类型。我可以在网上找到的唯一例子是将常量(“Cat”、“5.0”等)添加到数组列表中,这对我没有真正的帮助。
I put the gist of my code below. I've moved the arraylist off to its own class Bar and added an add method which just does arraylist.add(foo), if only as a crapshoot to make it work (it doesn't). I omitted the loop but it loops a number of times after the definitions, so the arraylist gets populated.
我把我的代码的要点放在下面。我已经将 arraylist 移到它自己的类 Bar 并添加了一个只执行 arraylist.add(foo) 的 add 方法,如果只是作为使其工作的废话(它没有)。我省略了循环,但它在定义之后循环了多次,因此填充了数组列表。
public class MainClass{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int a, b;
a = scanner.nextint();
b = scanner.nextint();
Foo foo = new Foo(a, b);
Bar.add(foo); //Complains here
}
}
Edit: Here is Bar explicitly
编辑:这里是 Bar 明确
import java.util.ArrayList;
public class Bar{
private ArrayList list = new ArrayList();
public void add(Foo foo){
list.add(foo);
}
}
If it helps, the object foo isn't changed after creation.
如果有帮助,则对象 foo 在创建后不会更改。
How do I get around this? Thanks in advance for any help.
我该如何解决这个问题?在此先感谢您的帮助。
回答by John Gardner
Without information about Bar, or the actual error it complains about, we can't help much.
如果没有关于 Bar 的信息,或者它抱怨的实际错误,我们无能为力。
I'd presume its because the Bar.add
method isn't static, or Bar
isn't static.
我认为它是因为该Bar.add
方法不是静态的,或者Bar
不是静态的。
// a class with a public static method that encapsulates a static list
public class Bar
{
static List<Foo> innerlist = new ArrayList<Foo>();
public static void add( Foo o )
{
innerList.add(o);
}
}
or do you intend "Bar" to be a static list member of something else?
或者你打算“Bar”成为其他东西的静态列表成员?
// a class with a static member
public class OtherClass
{
public static List<Foo> Bar = new ArrayList<Foo>();
}
then your code would need a OtherClass.Bar.add(o)
instead of just Bar.add(o);
那么你的代码需要一个OtherClass.Bar.add(o)
而不仅仅是 Bar.add(o);
回答by Aravind Yarram
You have 2 options
你有2个选择
option 1: create an instance of Bar and add foo to that instance. This is my preferred option
选项 1:创建 Bar 的实例并将 foo 添加到该实例。这是我的首选
Bar b=new Bar();
b.add(foo);
option 2: make your add(...) method static. this also means your "list" should also be static. both are bad.
选项 2:使您的 add(...) 方法静态。这也意味着您的“列表”也应该是静态的。两者都不好。