为什么我的输出是字节码,我如何得到字符串表示?

时间:2017-04-19 22:27:30

标签: java arraylist interface compareto

我已经用一个对象的实例填充了一个数组,使用compareTo方法对它进行了排序,我试着返回已排序的列表。

import java.util.Arrays;

public class test {


public static void main(String[] args) {
    Patients1 assetList = new Patients1[5];
    assetList = new Patients1[5];

    assetList[0] = new Patients1("VINTJAC001");
    assetList[1] = new Patients1("LEWGCOL002");
    assetList[2] = new Patients1("HENFBOR003");
    assetList[3] = new Patients1("ARTDMAN004");
    assetList[4] = new Patients1("KISCBIS005");

    Arrays.sort(assetList);
    System.out.println (Arrays.toString(assetList));
}
}

public class Patients1 implements Comparable<Patients1>{
private String patient_id;
public Patients1(String patient_id){
    this.patient_id=patient_id;
}
public String getPatient_id(){
    return patient_id;
}
@Override
public int compareTo(Patients1 p) {
    int result = 0;
    int compare = patient_id.compareTo(p.patient_id);
    if(compare < 0){
        result = -1;
    }
    else if (compare > 0){
        result = 1;
    }
    else if (compare == 0){
        result = 0;
    }
    return result;
}

}

然而,这是我得到的输出:

[samples.Patients1@15db9742, samples.Patients1@6d06d69c, 
samples.Patients1@7852e922, samples.Patients1@4e25154f, 
samples.Patients1@70dea4e] /*samples is the package name 
and Patients1 is the class that has the compareTo method*/

应该是:

[ARTDMAN004, HENFBOR003, KISCBIS005, LEWGCOL002, VINTJAC001] (sorted list)

我做错了什么或者只是不做?

1 个答案:

答案 0 :(得分:0)

您需要覆盖toString课程中的Patients1方法。