标签:cti set static com string tag 高级 obj system
创建Employee类,在类中定义三个属性:编号,姓名,年龄,然后在构造方法里初始化这三个属性,最后在实现接口中的定义的CompareTo方法,将对象按编号升序排列。
代码如下:(程序可能有些错误,方法是对的,见谅)
import java.awt.List;
import java.util.ArrayList;
import java.util,Collection;
import java.util,Collections;
import java.util,Iterator;
import java.util,Scanner;
class Employee implements omparable{
private int id;
private String name;
private int age;
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name-name;
}
public int getAge(){
return age;
}
public void setId(int age){
this.age=age;
}
Employee(int id,String name,int age){
this.id=id;this.name=name;this.age=age;
}
public int compareTo(Object e){
Employee o=(Employee)e;
if(this.id>o.id)return 1;
else if(this.id<o.id)return -1;return 0;
}
}
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
ArrayList list=new ArrayList();
for(int i=0;i<3;i++){
int id=in,nextInt();
String str=in.next();
int age=in.nextInt();
list.add(new Employee(id,str,age));
}
Collections.sort(list);
System.out.print("[");
Iterator it=list.iterator();
while(it.hasNext()){
Employee e=(Employee) it.next();
System.out.print("Id= "+e.getId+"Name= "+e.getName()+"Age= "+e.getAge());
if(it.hasNext())System.out.print(",“);
}
System.out.println("]");
}
}
标签:cti set static com string tag 高级 obj system
原文地址:https://www.cnblogs.com/mikerr/p/11067396.html