java 将数组列表作为参数传递给方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14778013/
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
Passing an arraylist as argument to method
提问by user1803704
I have a method which looks like this:
我有一个看起来像这样的方法:
public Person(String name, Person mother, Person father, ArrayList<Person> children) {
this.name=name;
this.mother=mother;
this.father=father;
this.children=children;
}
When trying to create a new person with children, however, I run in to problems:
然而,当我试图创造一个有孩子的新人时,我遇到了问题:
Person Toby = new Person("Toby", null, null, (Dick, Chester));
even though both Dick and Chester are defined further down. More specifically, it complains that neither Dick nor Chester can be resolved to a variable. Do I have to make a temporary ArrayList and pass that?
即使 Dick 和 Chester 的定义都进一步向下。更具体地说,它抱怨 Dick 和 Chester 都不能解析为变量。我是否必须制作一个临时的 ArrayList 并传递它?
Thank you.
谢谢你。
回答by Sotirios Delimanolis
Yes you wouldn't pass Dick
and Chester
to the constructor like that.
是的,您不会像那样传递Dick
和传递Chester
给构造函数。
You would, assuming you do have two Person objects named Dick
and Chester
:
您会,假设您确实有两个名为Dick
and 的Person 对象Chester
:
ArrayList<Person> children = new ArrayList<Person>();
children.add(Dick);
children.add(Chester);
Person Toby = new Person("Toby", null, null, children);
You're constructor is expecting an ArrayList object so that is what you have to pass it. Notation like the one you used, (Dick, Chester)
, doesn't make sense in Java.
您的构造函数需要一个 ArrayList 对象,因此您必须传递它。像您使用的符号(Dick, Chester)
, 在 Java 中没有意义。
回答by Hyman
You can either use varargs:
您可以使用可变参数:
public Person(String name, Person mother, Person father, Person... children) {
...
this.children = Arrays.asList(children);
}
Person p = new Person("Foo", Mother, Father, Dick, Chester);
回答by Eurig Jones
I would personally change the Person constructor to be:
我个人会将 Person 构造函数更改为:
public Person(String name, Person mother, Person father, Person... children)
{
}
The ... basically means the constructor will create its own array of Person objects so a call to it would be, for example:
... 基本上意味着构造函数将创建自己的 Person 对象数组,因此对它的调用将是,例如:
Person toby = new Person("Toby", null, null, childOne, childTwo, childThree);
Or:
或者:
Person toby = new Person("Toby", null, null);
回答by ATrubka
Your example doesn't look like a Java code. First of all you have to define Dick and Chester before using them. So they have to be created "above" Toby. Secondly, round brackets don't create you a list. You have to explicitly create an array list with this:
您的示例看起来不像 Java 代码。首先,您必须在使用 Dick 和 Chester 之前对其进行定义。因此,它们必须被创建在“托比之上”。其次,圆括号不会为您创建列表。您必须使用以下内容显式创建一个数组列表:
new ArrayList<Person>()
回答by Gaurav Agarwal
Incase you were not able to follow the comments on the question try this:
如果您无法关注该问题的评论,请尝试以下操作:
public Person(String name, Person mother, Person father, List<Person> children) {
this.name=name;
this.mother=mother;
this.father=father;
this.children=children;
}
Here I have changed the signature to take the last parameter as List type.
在这里,我更改了签名以将最后一个参数作为 List 类型。
Person Toby = new Person("Toby", null, null, Arrays.asList(Dick, Chester));
Also the children signature would have to be changed. The point here is to use a more abstract type.
儿童签名也必须更改。这里的重点是使用更抽象的类型。