2015-04-17 21 views
-2

爲什麼toString在我的代碼中不起作用?輸出應該是idChild []中的所有元素。ToString不起作用

錯誤:

child[Ljava.lang.String;@15db9742

public String[] onePointCrossover(int father, int mother) { 

    String linha1 = individualID.get(father);  
    idFather = linha1.split(" "); 
    String linha2 = individualDep.get(father); 
    depenFather= linha2.split(" "); 
    String linha3 = individualHour.get(father); 
    hourFather = linha3.split(" "); 

    String linhaA = individualID.get(mother);  
    idMother = linha1.split(" "); 
    String linhaB = individualDep.get(mother); 
    depenMother= linha2.split(" "); 
    String linhaC = individualHour.get(mother); 
    hourMother = linha3.split(" "); 

    String [] idChild = new String [idFather.length]; 
    int crossPoint = (int) (Math.random()*idFather.length); 

    for(int i=0; i<idFather.length; i++) 
    { 
     if (i<crossPoint) 
      idChild[i] = idFather[i]; 
     else 
      idChild [i] = idMother[i]; 
    } 

    System.out.println("child" + idChild.toString()); 
    return idChild;  
} 
+0

如果你想要不同於標準的行爲,你必須'重寫''toString()'方法。調用'idChild.toString()'只是將對象類型和位置作爲字符串提供給您。 –

回答

0

如果你想通過你的陣列中的所有孩子的循環,那麼你需要通過它循環,其他明智的您正試圖讀取對象的數組作爲字符串!

嘗試:

foreach (string s in idChild) 
    { 
     System.out.println(s); 
    } 
0

這是正路toString()作品(文檔here):在Object類的默認實現(和所有陣列)顯示類名,@符號和十六進制表示該對象的散列碼:

public String toString() { 
    return getClass().getName() + "@" + Integer.toHexString(hashCode()); 
} 

文檔說:

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object.

因此,程序員真的需要選擇「文本表示」的含義。

如果要打印數組中所有項的String表示形式,則必須遍歷它。