我想寫一個接受ArrayList(包含整數)作爲其參數之一的類的構造函數。稍後再實例化這個類時,我會傳遞一個適當的,預先填充的值列表,所以我不想在構造函數中創建一個空列表。使用ArrayList作爲構造函數的參數
不幸的是,當我嘗試編譯下面的代碼時,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
這些錯誤是什麼? – Dancrumb 2013-04-23 20:16:36
http://stackoverflow.com/questions/997482/does-java-support-default-parameter-values – 2013-04-23 20:18:33