打印列表时,这是乱码

时间:2012-09-29 22:41:16

标签: java

  

可能重复:
  Java Object default toString

为什么当我从文件阅读器打印我的列表时,它是[myServiceOrder @ 3bc1cac,myServiceOrder @ 32fe621e,myServiceOrder @ 5adbb9b9,myServiceOrder @ f7e4f49,myServiceOrder @ 2d874991,myServiceOrder @ ceee5f1,myServiceOrder @ 183a37d9]

public class myServiceOrder implements ServiceOrder, Comparable<myServiceOrder>{

private int number=0;
private String ownerName="";
private String make="";
private String model="";
private int year=0;

public myServiceOrder(int number, String ownerName, String make, String model,  int year) {
    this.number=number;
    this.ownerName=ownerName;
    this.make=make;
    this.model=model;
    this.year=year;
}

public myServiceOrder() {
    // TODO Auto-generated constructor stub
}

@Override
public void setOrderNum(int orderNumber) {
    number=orderNumber;
}

@Override
public void setYear(int year) {
    this.year=year;
}

@Override
public void setOwner(String ownerName) {
    this.ownerName=ownerName;
}

@Override
public void setMake(String make) {
    this.make=make;
}

@Override
public void setModel(String model) {
    this.model=model;

}

@Override
public String getOwner() {
    return ownerName;
}

@Override
public String getMake() {
    return make;
}

@Override
public String getModel() {
    // TODO Auto-generated method stub
    return  model;
}

@Override
public int getOrderNum() {
    // TODO Auto-generated method stub
    return number;
}

@Override
public int getYear() {
    // TODO Auto-generated method stub
    return year;
}

@Override
public String getMakeModelYear() {
    // TODO Auto-generated method stub
    return make+ " "+ model+ " "+ year+ " ";
}

@Override
public boolean equals(ServiceOrder otherServiceOrder) {
        if (getOrderNum()==otherServiceOrder.getOrderNum())
            return true;
        else
    return false;
}

@Override
public int compareTo(ServiceOrder otherServiceOrder, int key) {
    int comparisonResult=0;
    if(key==1)
    {
        if(getOrderNum()< otherServiceOrder.getOrderNum())
            comparisonResult= -1;
        if(getOrderNum()== otherServiceOrder.getOrderNum())
            comparisonResult= 0;
        if(getOrderNum()> otherServiceOrder.getOrderNum())
            comparisonResult= 1;
    }
        else if(key==2)
        {
            comparisonResult = getOwner().compareTo(otherServiceOrder.getOwner());                      
        }

        else if(key==3)
        {
         comparisonResult = getOwner().compareTo(otherServiceOrder.getOwner());
        }
    return comparisonResult;

}

@Override
public int compareTo(myServiceOrder arg0) {
    // TODO Auto-generated method stub
    return 0;
}

}


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;
import javax.swing.JOptionPane;


public class List extends LinkedList<myServiceOrder> {
private static  LinkedList<myServiceOrder> newList = new LinkedList();

 public  void  Print() throws Exception
 {
     System.out.println(newList);
 }
public   LinkedList<myServiceOrder> createServiceOrder(File inFile) throws Exception {
    int number=0;
     String ownerName="";
     String make="";
     String model="";
     int year=0;
     myServiceOrder serviceList = new myServiceOrder();    
        Scanner fileScan=new Scanner(inFile);
            while (fileScan.hasNext())
            {
                String ignore;

                number = fileScan.nextInt();
                //System.out.println(number);
                ignore = fileScan.nextLine(); // ignore the newline
                ownerName = fileScan.nextLine();
            //  System.out.println(ownerName);
                make = fileScan.nextLine();
        //      System.out.println(make);
                model = fileScan.nextLine();
            //  System.out.println(model);
                year = fileScan.nextInt();
        //      System.out.println(year);
                ignore = fileScan.nextLine(); // ignore the newline
                  serviceList = new myServiceOrder( number,  ownerName,  make,        model,  year);         
                  newList.add(serviceList);

            }
        fileScan.close();


     //   System.out.println(newList.viewAll());
        return newList; 
    }


}

好的,我明白了,我是我的密集。我还有第二个问题:我必须根据我的GUI选择三种不同的方式对列表进行排序,我选择了哪个选项,我假设我实现Comparable,但在我的compareTo界面中它是{{1 }}。如果sort方法只是Object o,我该如何使用该键。我应该尝试使用compareTo(Object o, int key)吗?如果我的Comparator如何告诉它在我的List类中按这种方式排序?

4 个答案:

答案 0 :(得分:7)

myServiceOrder类中toString()方法缺少覆盖的经典案例。

在实施中查看here示例。 This pageRohit's answer解释为什么您需要覆盖toString()。




直到现在为止,Argh还没有看到你的第二个问题:

有关使用Comparable界面与使用Comparator界面之间的差异,请参阅this questionthis question

答案 1 :(得分:3)

Java如何知道您希望如何打印myService对象?你可以通过覆盖toString告诉它:

@Override
public String toString() {
    return "myServiceObject#" + number + "[" + ownername + ", " + make + ", " + model + ", " + year + "]";
}

答案 2 :(得分:1)

打印的内容实际上是您打印的对象的哈希码,而不会覆盖toString方法。现在,因为您正在打印LinkedList,所以不能这样做。相反,您可以遍历列表并打印个别元素: -

public  void  Print() throws Exception
 {
    for (myServiceOrder so: newList) {
         System.out.println(so)
    }
 }

现在,由于serviceOrder本身就是一个对象..您需要覆盖该类中的toString() ..

@Override
public String toString() {
    return this.ownerName + this.make + "[" +  this.model + " - " + String.valueOf(this.year) + "]";            
}

答案 3 :(得分:1)

System.out.println(newList);

这会自动调用toString()类的LinkedList方法,而toString()类又会在列表中的每个引用上调用ServiceOrder(在这种情况下为toString()个对象)。由于您尚未提供自己的Object方法,因此使用myServiceOrder@3bc1cac中的默认方法。这给出了有趣的输出toString(),这是Java打印引用变量的默认方式。如果您希望看到其他内容,您需要通过在ServiceOrder课程中实施{{1}}来告诉Java如何执行此操作。