java 使用 ArrayList 作为构造函数的参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16178355/
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
Using an ArrayList as Parameter of Constructor
提问by James Scholes
I'm trying to write a constructor for a class which accepts an ArrayList (containing integers) as one of it's arguments. When instantiating this class later, I will pass an appropriate, pre-populated list of values, so I don't want to create an empty list inside the constructor.
我正在尝试为一个类编写一个构造函数,该类接受一个 ArrayList(包含整数)作为它的参数之一。稍后实例化此类时,我将传递一个适当的、预先填充的值列表,因此我不想在构造函数中创建一个空列表。
Unfortunately, when I try to compile the below code, Java spits out five errors, all related to line 23 (my constructor's function definition). Any advice would be appreciated:
不幸的是,当我尝试编译下面的代码时,Java 吐出五个错误,都与第 23 行(我的构造函数的函数定义)有关。任何意见,将不胜感激:
/*
* SumGenerator
*
* @author James Scholes
*/
import java.util.ArrayList;
import java.util.Random;
public class SumGenerator
{
// Some initial variables
public int timesTable;
public int multiple;
/*
* Constructor
*
* @param timesTable(int): The times table to use for sum generation
* @param limit(int): The highest multiple to use in sum generation
* @param previousMultiples(ArrayList<Integer>): The previously used multiples to avoid sum duplication
*/
public SumGenerator(int timesTable, int limit = 10, ArrayList<Integer> previousMultiples)
{
this.timesTable = timesTable;
Random randomGenerator = new Random();
// Create a list to store our multiples
ArrayList<Integer> multiples = new ArrayList<Integer>();
// and add our multiples to it, only if
// they haven't been used before
for(int i = timesTable; i <= limit; i++)
{
if(previousMultiples.contains(i))
{
continue;
}
else
{
multiples.add(i);
}
}
this.multiple = multiples.get(randomGenerator.nextInt(multiples.size()));
}
}
SumGenerator.java:23: error: ')' expected
public SumGenerator(int timesTable, int limit = 10, ArrayList<Integer> previousMultiples)
^
SumGenerator.java:23: error: illegal start of type
public SumGenerator(int timesTable, int limit = 10, ArrayList<Integer> previousMultiples)
^
SumGenerator.java:23: error: <identifier> expected
public SumGenerator(int timesTable, int limit = 10, ArrayList<Integer> previousMultiples)
^
SumGenerator.java:23: error: ';' expected
public SumGenerator(int timesTable, int limit = 10, ArrayList<Integer> previousMultiples)
^
SumGenerator.java:23: error: <identifier> expected
public SumGenerator(int timesTable, int limit = 10, ArrayList<Integer> previousMultiples)
^
5 errors
回答by djechlin
Java doesn't support default arguments.
Java 不支持默认参数。
回答by rgettman
You can't supply default values for parameters in Java: int limit = 10
. To work around, supply overloaded constructors. One doesn't have limit
and will supply the other with the default value.
您不能为 Java 中的参数提供默认值:int limit = 10
. 要解决此问题,请提供重载的构造函数。一个没有limit
并且将为另一个提供默认值。
public SumGenerator(int timesTable, ArrayList<Integer> previousMultiples)
{
this(timesTable, 10, previousMultiples);
}
public SumGenerator(int timesTable, int limit, ArrayList<Integer> previousMultiples)
{
// Your constructor here.
}
回答by Jesper
public SumGenerator(int timesTable, int limit = 10, ArrayList<Integer> previousMultiples)
Remove the = 10
in int limit = 10
. Java does not support default values for constructor or method arguments.
删除= 10
在int limit = 10
. Java 不支持构造函数或方法参数的默认值。