可能重复:
Java 对象默认 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
界面中它是compareTo(Object o, int key)
. 如果排序方法只是 Object o,我如何使用该键。我应该尝试使用Comparator
吗?如果我key=1
怎么能告诉它在我的 List 类中以这种方式排序?