2014-03-26 55 views
0

我試圖抓住從一組自定義對象(空間)的隨機元素,並在此過程中接收到錯誤。的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

對象的數組不能轉換到FOOS的陣列。您將需要遍歷數組並分別投射每個成員。 (更重要的是,正確使用泛型/多態性與不投。)

+0

你明白我懷疑+1和謝謝:) –

1

因爲as the docs point outtoArray()返回Object[]

您需要使用othertoArray(T[] a)如果你想推斷類型:

s = spaceSet.toArray(new Space[0])[rand.nextInt(spaceSet.size())]; 

(在這種情況下提供的陣列僅用於類型推斷; toArray()正在返回適當尺寸的新的Space[]

0

嘗試使用隨機大小/空間時。它總是一個更好的主意,使用一個地圖,而不是陣列向下幾個層次。這裏有一個類似的例子,你可以用最小的鍋爐板代碼設置/獲取值。

import java.util.*; 

public class SmartPropertyReader { 
    private SmartPropertyReader parent; 

    protected final String PRESERVE_MAP = "PRESERVE_MAP"; 
    protected final String PRESERVE_LIST = "PRESERVE_LIST"; 
    private String preserveType; 

    protected Map<String, SmartPropertyReader> map = new HashMap<String, SmartPropertyReader>(); 
    protected Object value; 
    protected List<SmartPropertyReader> list = new ArrayList<SmartPropertyReader>(); 

    public SmartPropertyReader(){ 
     this.parent = null; 
    } 

    public SmartPropertyReader(SmartPropertyReader parent, String preserveType) { 
     this.parent = parent; 
     this.preserveType = preserveType; 
    } 


    public SmartPropertyReader get(String key) { 
     SmartPropertyReader subProp = map.get(key); 
     if(subProp == null){ 
      subProp = new SmartPropertyReader(this, PRESERVE_MAP); 
      map.put(key, subProp) ; 
     } 
     return subProp; 
    } 


    public void setValue(Object passedValue) { 
     list.clear(); 
     map.clear(); 

     if(passedValue instanceof SmartPropertyReader){ 
      this.map = ((SmartPropertyReader) passedValue).map; 
      this.list = ((SmartPropertyReader) passedValue).list; 
     }else{ 
      this.value = passedValue; 
     } 

     SmartPropertyReader currPropertyReader = this; 
     while(currPropertyReader != null){ 
      String preserveType = currPropertyReader.preserveType; 
      currPropertyReader = currPropertyReader.parent; 
      if(PRESERVE_MAP.equals(preserveType)){ 
       currPropertyReader.clearList(); 
       currPropertyReader.clearValue(); 
      }else if(PRESERVE_LIST.equals(preserveType)){ 
       currPropertyReader.clearValue(); 
       currPropertyReader.clearMap(); 
      } 
     } 
    } 

    protected void clearList(){ 
     list.clear(); 
    } 

    protected void clearMap(){ 
     map.clear(); 
    } 

    protected void clearValue(){ 
     this.value = null; 
    } 

    public Object getValue() { 
     if(this.value == null){ 
      SmartPropertyReader currPropertyReader = parent; 
      while(currPropertyReader != null){ 
       String preserveType = currPropertyReader.preserveType; 
       if(PRESERVE_MAP.equals(preserveType)){ 
        currPropertyReader.clearMap(); 
       }else if(PRESERVE_LIST.equals(preserveType)){ 
        currPropertyReader.clearList(); 
       } 
       currPropertyReader = currPropertyReader.parent; 
      } 
     } 
     return this.value; 
    } 


    public SmartPropertyReader get(int index) { 
     while(list.size() <= index){ 
      list.add(null); 
     } 
     SmartPropertyReader subProp = list.get(index); 
     if(subProp == null){ 
      subProp = new SmartPropertyReader(this, PRESERVE_LIST); 
     } 

     list.set(index, subProp); 
     return subProp; 
    } 


    public String toString(){ 
     String retString = ""; 
     if(value != null){ 
      retString = value.toString(); 
      if(value instanceof String){ 
       retString = "\"" + retString + "\""; 
      } 
     } 

     if(list.size() > 0){ 
      String listStr = listString(); 
      if(!listStr.equals("")) 
       retString = "[" + listString() + "]"; 
     } 

     if(map.size() > 0){ 
      String mapStr = mapString(); 
      if(!mapStr.equals("")) 
       retString = "{" +mapString() + "}"; 
     } 

     return retString; 
    } 


    private String listString(){ 
     String str = ""; 
     boolean first = true; 
     for(SmartPropertyReader rblt: list){ 
      if(rblt != null){ 
       String subStr = rblt.toString(); 
       if(!subStr.equals("")){ 
        if(!first) 
         str += ", "; 
        str += subStr; 
        first = false; 
       } 
      } 
     } 
     return str; 
    } 

    private String mapString(){ 
     String str =""; 
     boolean first = true; 
     for(String key: map.keySet()){ 
      SmartPropertyReader rblt = map.get(key); 
      if(rblt != null){ 
       String subStr = rblt.toString(); 
       if(!subStr.equals("")){ 
        if(!first) 
         str += ", "; 
        str += "\""+key + "\": " + subStr; 
        first = false; 
       } 
      } 
     } 
     return str; 
    } 

    public static void main(String[] args) { 
     SmartPropertyReader propertyReader = new SmartPropertyReader(); 
     propertyReader.get("Test").get("2nd key").get("A number value").setValue(10); 
     propertyReader.get("Test").get("a key").get(1).get(2).setValue(100); 
     propertyReader.get("Test").get("a key").get(1).get(3).setValue(100.345); 
     propertyReader.get("Test").get("a key").get(2).get("Nice").setValue("Nice value in the stack"); 

     propertyReader.get("Test").get("a key").get(5).setValue("This would work too"); 
     System.out.println(propertyReader.toString()); 
     System.out.println(propertyReader.get("Test").get("2nd key").get("A number value").getValue()); 
     System.out.println(propertyReader.get("Test").get("a key").get(1).get(2).getValue()); 
     System.out.println(propertyReader.get("Test").get("a key").get(1).get(3).getValue()); 
     System.out.println(propertyReader.get("Test").get("a key").get(2).get("Nice").getValue()); 
     System.out.println(propertyReader.get("Test").get("a key").get(5).getValue()); 
    } 

}