Java 将arraylist作为参数传递给方法

Java 将arraylist作为参数传递给方法,java,arraylist,arguments,Java,Arraylist,Arguments,我有一个方法如下所示: public Person(String name, Person mother, Person father, ArrayList<Person> children) { this.name=name; this.mother=mother; this.father=father; this.children=children; } 尽管狄克和切斯特的定义都更深。更具体地说,它抱怨迪克和切斯特都不能解析为变量。我需要做一个临

我有一个方法如下所示:

public Person(String name, Person mother, Person father, ArrayList<Person> children) {
    this.name=name;
    this.mother=mother;
    this.father=father;
    this.children=children;
}
尽管狄克和切斯特的定义都更深。更具体地说,它抱怨迪克和切斯特都不能解析为变量。我需要做一个临时的ArrayList并通过它吗


谢谢。

是的,你不会像那样把
Dick
Chester
传递给构造函数

假设有两个Person对象,分别名为
Dick
Chester

ArrayList<Person> children = new ArrayList<Person>();
children.add(Dick);
children.add(Chester);

Person Toby = new Person("Toby", null, null, children);
ArrayList children=new ArrayList();
加上(迪克);
添加(切斯特);
Person Toby=新人(“Toby”,空,空,子女);

您的构造函数需要一个ArrayList对象,所以您必须传递它。像您使用的符号,
(Dick,Chester)
,在Java中没有意义。

是的,您不会像那样将
Dick
Chester
传递给构造函数

假设有两个Person对象,分别名为
Dick
Chester

ArrayList<Person> children = new ArrayList<Person>();
children.add(Dick);
children.add(Chester);

Person Toby = new Person("Toby", null, null, children);
ArrayList children=new ArrayList();
加上(迪克);
添加(切斯特);
Person Toby=新人(“Toby”,空,空,子女);

您的构造函数需要一个ArrayList对象,所以您必须传递它。类似于您使用的符号,
(Dick,Chester)
,在Java中没有意义。

我个人将Person构造函数更改为:

public Person(String name, Person mother, Person father, Person... children)
{
}
这个。。。基本上意味着构造函数将创建自己的Person对象数组,因此对它的调用将是,例如:

Person toby = new Person("Toby", null, null, childOne, childTwo, childThree);
或:


我个人会将该人员更改为:

public Person(String name, Person mother, Person father, Person... children)
{
}
这个。。。基本上意味着构造函数将创建自己的Person对象数组,因此对它的调用将是,例如:

Person toby = new Person("Toby", null, null, childOne, childTwo, childThree);
或:


您可以使用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);

您可以使用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);

您的示例看起来不像Java代码。 首先,在使用迪克和切斯特之前,你必须对他们下定义。因此,它们必须“高于”托比。 第二,圆括号不会为您创建一个列表。必须使用以下命令显式创建数组列表:

new ArrayList<Person>()
newarraylist()

您的示例看起来不像Java代码。 首先,在使用迪克和切斯特之前,你必须对他们下定义。因此,它们必须“高于”托比。 第二,圆括号不会为您创建一个列表。必须使用以下命令显式创建数组列表:

new ArrayList<Person>()
newarraylist()

如果您无法按照问题的注释进行操作,请尝试以下操作:

public Person(String name, Person mother, Person father, List<Person> children) {
    this.name=name;
    this.mother=mother;
    this.father=father;
    this.children=children;
}
此外,还必须更改儿童签名。
这里的要点是使用更抽象的类型。

如果您无法按照问题的注释进行操作,请尝试以下操作:

public Person(String name, Person mother, Person father, List<Person> children) {
    this.name=name;
    this.mother=mother;
    this.father=father;
    this.children=children;
}
此外,还必须更改儿童签名。

这里的要点是使用更抽象的类型。

“我必须创建一个临时数组列表并传递它吗?”是的。你现在拥有的不是有效的java代码。用于比较。然后,您可以使用数组的
asList
方法传递相同类型的对象。@GauravAgarwal:
asList()
只有在他将构造函数更改为接受
List
而不是
ArrayList
@GauravAgarwal:我完全同意。但是必须提到这一点,因为只有当他将
列表更改为
列表时,使用
数组.asList()
才会起作用。“我必须创建一个临时数组列表并传递它吗?”是的。你现在拥有的不是有效的java代码。用于比较。然后,您可以使用数组的
asList
方法传递相同类型的对象。@GauravAgarwal:
asList()
只有在他将构造函数更改为接受
List
而不是
ArrayList
@GauravAgarwal:我完全同意。但是必须提到,因为只有当他将数组更改为
List
时,使用
Arrays.asList()
才会起作用。除非他将Person的构造函数更改为接受List而不是ArrayList,否则这不会起作用。其次,Dick和Chester是Person实例,不是字符串。只有当他将构造函数更改为接受
List
而不是
ArrayList
时,这才有效。另外,您可以只编写
数组.asList(childOne,childTwo)
,因为它是一个varargs方法,所以您不必手动创建数组。然后您只需更改构造函数。这是他的密码,不是第三方。@Amorgos:是的,但你必须在回答中提到它!除非他将Person的构造函数更改为接受列表而不是ArrayList,否则这将不起作用。其次,Dick和Chester是Person实例,而不是字符串。只有当他将构造函数更改为接受
列表而不是
ArrayList
时,这才起作用。另外,您可以只编写
数组.asList(childOne,childTwo)
,因为它是一个varargs方法,所以您不必手动创建数组。然后您只需更改构造函数。这是他的密码,不是第三方。@Amorgos:是的,但你必须在回答中提到它!这是可行的,但我必须在代码顶部创建ArrayList,并在初始化后添加子元素。Python在这方面要简单得多,至少应该可以这样做:persontoby=newperson(“Toby”,null,null,ArrayList(Dick,Chester));即使在Java中,您也可以通过
新建ArrayList(Arrays.asList(newPerson[]{Dick,Chester})]
来实现这一点,更详细,但语义上是等效的。老实说,即使您可以做到,也不意味着您应该这样做。尝试在代码中分离不同的操作,不要在一行中完成所有操作。可读性是关键。这是可行的,但我必须在代码顶部创建ArrayList,并在初始化后添加子元素。Python在这方面要简单得多,至少应该可以这样做:persontoby=newperson(“Toby”,null,null,ArrayList(Dick,Chester));您甚至可以通过
newarraylist(数组)在Java中实现这一点