当前位置:编程学习 > JAVA >>

Java中如何求泛型的最大值?

public class testSort {
public static void main(String[] args) {
Student stu1=new Student("zhangsan",18);
Student stu2=new Student("lisi",20);
Student stu3=new Student("wangba",31);
Student stu4=new Student("zhaoliu",17);

List<Student> stu=new ArrayList<Student>();
stu.add(stu1);
stu.add(stu2);
stu.add(stu3);
stu.add(stu4);

}
}
}
比如求如上4个人中年龄最大者 怎么求啊? --------------------编程问答--------------------



public class testSort {
    public static void main(String[] args) {
Student stu1 = new Student("zhangsan", 18);
Student stu2 = new Student("lisi", 20);
Student stu3 = new Student("wangba", 31);
Student stu4 = new Student("zhaoliu", 17);

List<Student> stu = new ArrayList<Student>();
stu.add(stu1);
stu.add(stu2);
stu.add(stu3);
stu.add(stu4);

int temp = 0;
for(int i=1; i<stu.size(); i++){
    if(stu.get(temp).age<stu.get(i).age) temp=i;
}
System.out.println(stu.get(temp).name + stu.get(temp).age );

/* for(int i=1; i<stu.size(); i++){
    if(stu.get(temp).getAge()<stu.get(i).getAge() temp=i;
}
System.out.println(stu.get(temp).getName() + stu.get(temp).getAge());
*/
    }
}


--------------------编程问答-------------------- 如果你的age是私有变量 
我建议在Student类里面实现comparable接口,重写compareTo方法
--------------------编程问答--------------------
public class testSort {
    public static void main(String[] args) {
        Student stu1=new Student("zhangsan",18);
        Student stu2=new Student("lisi",20);
        Student stu3=new Student("wangba",31);
        Student stu4=new Student("zhaoliu",17);

        List<Student> stu=new ArrayList<Student>();
        stu.add(stu1);
        stu.add(stu2);
        stu.add(stu3);
        stu.add(stu4);
        
        Collections.sort(stu, new Comparator<Student>() {
            public int compareTo(Student s1, Student s2) {
                if (s1.age == s2.age) {return 0;}
                return s2.age - s1.age;
            }
        });

        System.out.println(stu.get(0));
    }
}
--------------------编程问答-------------------- public class testSort {
    public static void main(String[] args) {
    Student stu1 = new Student("zhangsan", 18);
    Student stu2 = new Student("lisi", 20);
    Student stu3 = new Student("wangba", 31);
    Student stu4 = new Student("zhaoliu", 17);

    List<Student> stu = new ArrayList<Student>();
    stu.add(stu1);
    stu.add(stu2);
    stu.add(stu3);
    stu.add(stu4);
    
    int temp = 0;
    for(int i=1; i<stu.size(); i++){
        if(stu.get(temp).age<stu.get(i).age) temp=i;
    }    
    System.out.println(stu.get(temp).name + stu.get(temp).age );
    
/*    for(int i=1; i<stu.size(); i++){
        if(stu.get(temp).getAge()<stu.get(i).getAge() temp=i;
    }    
    System.out.println(stu.get(temp).getName() + stu.get(temp).getAge());
*/    
    }
}


--------------------编程问答--------------------


import java.util.ArrayList;
import java.util.List;

//求出年龄最大的值
public class Test {
public static void main(String[] args) {
Student stu1 = new Student("zhangsan", 18);
Student stu2 = new Student("lisi", 20);
Student stu3 = new Student("wangba", 31);
Student stu4 = new Student("zhaoliu", 17);
List<Student> stu = new ArrayList<Student>();
stu.add(stu1);
stu.add(stu2);
stu.add(stu3);
stu.add(stu4);
int temp=0;
for (int i =0; i <stu.size()-1; i++) {
temp=stu.get(i).getAge()>stu.get(i+1).getAge()?i:(i+1);
}
System.out.println(stu.get(temp).getName()+"\t"+stu.get(temp).getAge());
}
}

class Student {
private String name;
private int age;
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

}
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,