我想知道如何在以下场景中生成我的数组的克隆:
private List<TestObject> testList = Collections.synchronizedList(new ArrayList<TestObject>());
...
public synchronized List<TestObject> getAllTestObjects(){
return testList.clone();
//I have tried casting to List<TestObject>
}
对于List()未定义的clone()抱怨。但我不确定如何最好地解决它?
非常感谢任何帮助。
答案 0 :(得分:2)
您需要使用ArrayList
public synchronized List<TestObject> getAllTestObjects(){
return new ArrayList<TestObject>(testList);
}
答案 1 :(得分:0)
这应该有效:
private static class TestObject {
}
private TestObject[] objects = new TestObject[10];
private List<TestObject> testList =
Collections.synchronizedList(new ArrayList<TestObject>(Arrays.asList(objects)));