Java里面对ArrayList 里面的类的对象进行排序应该如何实现? 现在要求对Car里面的location进行排序。
publicclassCar{privateStringsize;publicvoidsetSize(Stringsize){this.size=size;}public...
public class Car {
private String size;
public void setSize(String size){
this.size=size;
}
public String getSize(){
return size;
}
private String location;
public void setLocation(String location){
this.location=location;
}
public String getLocation(){
return location;
}
public String toString() {
return "Car assigned "+location+" "+size;
}
}
Tester 里面是这么读取,存储carlist的。
Scanner in = new Scanner(System.in);
System.out.println("Enter Car info:");
String readstring =in.nextLine();
System.out.println("\n");
String[] readinfo = readstring.split(" ");
int i=0;
int j = 0;
while(in.hasNextLine()&&readinfo[0].equals("Car"))//Case: when scan the first arg[0] is "Car"
{ //store each car's information with corresponding size and location into list
String size = readinfo[1];
String location = readinfo[2];
Car car1 = new Car();
car1.setSize(size);
car1.setLocation(location);
carlist.add(car1);
car1= (Car)carlist.get(i);
System.out.println(car1);
readstring =in.nextLine();
readinfo = readstring.split(" ");
i++;
} 展开
private String size;
public void setSize(String size){
this.size=size;
}
public String getSize(){
return size;
}
private String location;
public void setLocation(String location){
this.location=location;
}
public String getLocation(){
return location;
}
public String toString() {
return "Car assigned "+location+" "+size;
}
}
Tester 里面是这么读取,存储carlist的。
Scanner in = new Scanner(System.in);
System.out.println("Enter Car info:");
String readstring =in.nextLine();
System.out.println("\n");
String[] readinfo = readstring.split(" ");
int i=0;
int j = 0;
while(in.hasNextLine()&&readinfo[0].equals("Car"))//Case: when scan the first arg[0] is "Car"
{ //store each car's information with corresponding size and location into list
String size = readinfo[1];
String location = readinfo[2];
Car car1 = new Car();
car1.setSize(size);
car1.setLocation(location);
carlist.add(car1);
car1= (Car)carlist.get(i);
System.out.println(car1);
readstring =in.nextLine();
readinfo = readstring.split(" ");
i++;
} 展开
4个回答
展开全部
重载equal方法上头的已经给出了方法!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
实现Comparable接口,并且重写compareTo方法
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
重载EQUAL方法
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询