개체 비교
2444 단어 대상
//package Comparator;
import java.util.*;
import java.io.*;
class Student implements Comparator
{
int age;
String name;
Student()
{
}
Student(int age,String name)
{
this.age=age;
this.name=name;
}
public int compare(Object o1,Object o2)// ?vs? this vs o
{
return 0;
}
public String toString()
{
return " :"+name+" :"+age+"
";
}
public Comparator getComparator1()
{
return new Comparator()
{
public int compare(Object o1,Object o2)
{
Student oo1=(Student)o1;
Student oo2=(Student)o2;
if(oo1.age>oo2.age)return 1;
else if(oo1.age<oo2.age)return -1;
else
{
if(oo1.name.compareTo(oo2.name)>0)return 1;
else if(oo1.name.compareTo(oo2.name)<0)return -1;
else return 0;
}
};
};
}
public Comparator getComparator2()
{
return new Comparator()
{
public int compare(Object o1,Object o2)
{
Student oo1=(Student)o1;
Student oo2=(Student)o2;
if(oo1.name.compareTo(oo2.name)>0)return 1;
else if(oo1.name.compareTo(oo2.name)<0)return -1;
else return 0;
};
};
}
};
class ArraysTest1
{
public static void main(String[] args) throws Exception
{
Student s=new Student();
Student [] b={new Student(18,"zhang san"),new Student(16,"yan zi"),new Student(22,"zhang fei"),
new Student(22,"xiao yan"),new Student(18,"Zhang san"),new Student(16,"fei zi ")};
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(" (1: 2: ))");
String a=br.readLine();
if(a.equals("1"))//
{
Arrays.sort(b,s.getComparator1());
System.out.println(Arrays.toString(b));
}
else if(a.equals("2"))//
{
Arrays.sort(b,s.getComparator2());
System.out.println(Arrays.toString(b));
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
List 컬렉션 객체에서 서로 다른 속성 크기로 정렬된 인스턴스인스턴스는 다음과 같습니다. 테스트: 출력 결과는 다음과 같습니다. 두 번째 방법은 컬렉션에 따라.sort 재부팅 방법(예: 마스터 클래스에서 이렇게 작성하면 됩니다. 출력 결과는 다음과 같습니다. 전자의 코드 구조...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.