2015-04-17 23 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表示形式,则必须遍历它。