为什么我的输出是字节码,我如何获得字符串表示形式?
Why is my output in byte-code and how do I get the string representation?
我用一个对象的实例填充了一个数组,使用 compareTo 方法对它进行了排序,然后我尝试 return 排序后的列表。
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)
我做错了什么或没有做什么?
您需要覆盖 Patients1
class 中的 toString
方法。
我用一个对象的实例填充了一个数组,使用 compareTo 方法对它进行了排序,然后我尝试 return 排序后的列表。
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)
我做错了什么或没有做什么?
您需要覆盖 Patients1
class 中的 toString
方法。