定义一个泛型类,定义泛型T,存储咱们的最大值最小值,重载构造函数,存储最大最小值,重写toString方法;
package person.xsc.practice;
public class Num<T> {
//定义最大最小值
public T max;
public T min;
public Num() {
}
//构造函数(有参)
public Num(T max, T min) {
this.max = max;
this.min = min;
}
public T getMax() {
return max;
}
public void setMax(T max) {
this.max = max;
}
public T getMin() {
return min;
}
public void setMin(T min) {
this.min = min;
}
@Override
public String toString() {
return "Num [max=" + max + ", min=" + min + "]";
}
}
定义一个泛型方法比较最大最小值,传入我们的泛型数组
package person.xsc.practice;
public class GeneralCompareMethod {
public <T extends Comparable<T>> Num<T> getMaxMin(T[]num){
T max = num[0];
T min = num[0];
for (int i = 0; i < num.length; i++) {
if(max.compareTo(num[i])<0) {
max=num[i];
}
if(min.compareTo(num[i])>0) {
min=num[i];
}
}
return new Num<T>(max,min);
}
}
定义一个studentII类,用于对象数组比较,实现 Comparable接口,并重写compareTo方法。
package person.xsc.practice;
public class StudentII implements Comparable{
private int number=0; //学号
private String name=""; //学生姓名
private String gender=""; //性别
public StudentII() {
}
public StudentII(int number, String name, String gender) {
this.number = number;
this.name = name;
this.gender = gender;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "StudentII [number=" + number + ", name=" + name + ", gender=" + gender + "]";
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
StudentII student=(StudentII)o;
if(this.number==student.number){
return 0; //如果学号相同,那么两者就是相等的
}else if(this.number>student.getNumber()){
return 1; //如果这个学生的学号大于传入学生的学号
}else{
return -1; //如果这个学生的学号小于传入学生的学号
}
}
}
定义测试类,数组实例化,调用比较大小方法。
package person.xsc.practice;
public class NumTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
GeneralCompareMethod gcm=new GeneralCompareMethod();
System.out.println("=======整型数组比较=======");
Integer[]integerArray= {1,8,34,13,56};
System.out.println(gcm.getMaxMin(integerArray));
System.out.println("=======字符型数组比较=======");
Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };
System.out.println(gcm.getMaxMin(charArray));
System.out.println("=======双精度型数组比较=======");
Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };
System.out.println(gcm.getMaxMin(doubleArray));
System.out.println("=======StudentII对象数组比较=======");
StudentII[] stuArray= {new StudentII(1,"张三","男"),new StudentII(5,"李四","男"),new StudentII(2,"夏六","女")};
System.out.println(gcm.getMaxMin(stuArray));
System.out.println("=======字节型数组比较=======");
Byte[] byteArray = {'a','b','c','d'};
System.out.println(gcm.getMaxMin(byteArray));
}
}
=======整型数组比较=======
Num [max=56, min=1]
=======字符型数组比较=======
Num [max=O, min=E]
=======双精度型数组比较=======
Num [max=4.4, min=1.1]
=======StudentII对象数组比较=======
Num [max=StudentII [number=5, name=李四, gender=男], min=StudentII [number=1, name=张三, gender=男]]
=======字节型数组比较=======
Num [max=100, min=97]