我試圖抓住從一組自定義對象(空間)的隨機元素,並在此過程中接收到錯誤。的Java - 錯誤鑄造Set.toArray()爲Object []
Space[][][][] spaces = new Space[dim][dim][dim][dim];
Set<Space> spaceSet = new HashSet<Space>();
for(int i=0; i<dim; i++)
for(int j=0; j<dim; j++)
for(int k=0; k<dim; k++)
for(int l=0; l<dim; l++) {
spaces[i][j][k][l] = new Space(i,j,k,l);
spaceSet.add(spaces[i][j][k][l]);
}
...
Space s = null;
...
s = (Space[])spaceSet.toArray()[rand.nextInt(spaceSet.size())]; //This line throws the error
錯誤:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [LSpace;
at Maze.generatePath(Maze.java:45)
at Maze4D.main(Maze4D.java:15)
你明白我懷疑+1和謝謝:) –