public class Employee {
private String name;
개인 int 나이;
개인 장기 급여;
public String getName() {
return name;
}
public int getAge() {
나이 반환;
}
public long getSalary() {
급여 반환;
}
public Employee(문자열 이름, int 나이, int 급여) {
this.name = 이름;
this.age = 나이;
this.salary = 급여;
}
@Override
//이 메서드는 직원 정보를 표시합니다
public String toString() {
return "[name=" + this.name + ", age=" + this.age + ", salary=" +
this.salary + "]";
}
}
import java.util.ArrayList;Collections.sort()은 Comparable 및 Comparator 인터페이스를 모두 사용하지 않는 한 작동하지 않는 객체의 ArrayList에서 수행되기 때문에 문제를 일으킵니다. 컴파일 오류가 나타납니다.
import java.util.Collections;
public class ArrayListComparable {
public static void main(String[] args) {
ArrayList< 직원> arraylist = 새로운 ArrayList< 직원> ();
arraylist.add(new Employe("mateo", 32, 20000));
arraylist.add(new Employe("katia", 26, 10000));
arraylist.add(new Employe("aline", 30, 40000));
arraylist.add(new Employe("살림", 28, 35000));
System.out.println("정렬된 직원 목록:\n"+Collections.sort(arraylist));
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The operator + is undefined for the argument type(s) String, void
Bound mismatch: Collections 유형의 제네릭 메서드 sort(List)는 인수 (ArrayList )에 적용할 수 없습니다. 유추된 형식 Employe는 제한된 매개 변수를 대체할 수 없습니다 >
at ArrayListComparable.main(ArrayListComparable.java:14)
public class Employe는 Comparable을 구현합니다< 직원> {이제 Collections.sort() : < / span>
개인 문자열 이름;
개인 int 나이;
개인 장기 급여;
public String getName() {
return name;
}
public int getAge() {
나이 반환;
}
public long getSalary() {
급여 반환;
}
public Employee(문자열 이름, int 나이, int 급여) {
this.name = 이름;
this.age = 나이;
this.salary = 급여;
}
@Override
//이 메서드는 직원 정보를 표시합니다
public String toString() {
return "[name=" + this.name + ", age=" + this.age + ", salary=" +
this.salary + "]";
}
@Override
public int compareTo(Employee emp) {
//직원을 연령별로 순서대로 정렬 croiddant
//해당 직원의 나이
//가 개체보다 작거나 같거나 크면 음수, 0 또는 양의 정수를 소급합니다
return (this.age - emp.age);
}
}
가져 오기 java.util.ArrayList;이 코드를 실행하면 다음과 같은 결과가 나타납니다.
import java.util.Collections;
public class ArrayListComparable {
public static void main(String[] args) {
ArrayList< 직원> arraylist = 새로운 ArrayList< 직원> ();
arraylist.add(new Employe("mateo", 32, 20000));
arraylist.add(new Employe("katia", 26, 10000));
arraylist.add(new Employe("aline", 30, 40000));
arraylist.add(new Employe("살림", 28, 35000));
Collections.sort(배열 목록);
System.out.println("연령별로 정렬된 직원 목록:\n");
for(직원 e:arraylist)
System.out.println(e);
}
}
연령별 직원 목록:
[name=katia, age=26, salary=10000]
[name=salim, 나이=28, 급여=35000]
[이름=aline, 나이=30, 급여=40000]
[이름=mateo, 나이=32, 급여=20000]
import java.util.Comparator;테스트 클래스 main:
public class Employee {
private String name;
개인 int 나이;
개인 장기 급여;
public String getNom() {
반환 이름;
}
public int getAge() {
나이 반환;
}
public long getSalary() {
급여 반환;
}
public Employee(문자열 이름, int 나이, int 급여) {
this.name = 이름;
this.age = 나이;
this.salary = 급여;
}
@Override
//이 메서드는 직원 정보를 표시합니다
public String toString() {
return "[name=" + this.name + ", age=" + this.age + ", salary=" +
this.salary + "]";
}
/*
* 이름별로 직원을 정렬하기 위한 비교기
*/
public static 비교기< 직원> ComparatorName = 새 비교기< 직원> () {
@Override
public int compare(직원 e1, 직원 e2) {
return e1.getName().compareTo(e2.getName());
}
};
/*
* 직원을 연령별로 정렬하기 위한 비교기
*/
public static 비교기< 직원> ComparatorAge = 새 비교기< 직원> () {
@Override
public int compare(직원 e1, 직원 e2) {
return (int) (e1.getAge() - e2.getAge());
}
};
/*
* 급여를 기준으로 직원을 정렬하기 위한 비교기
*/
public static 비교기< 직원> ComparatorSalary = 새 비교기< 직원> () {
@Override
public int compare(직원 e1, 직원 e2) {
return (int) (e1.getSalary() - e2.getSalary());
}
};
}
import java.util.ArrayList;이 코드를 실행하면 다음과 같은 결과가 나타납니다.
import java.util.Collections;
public class main{
public static void main(String[] args) {
ArrayList< 직원> arraylist = 새로운 ArrayList< 직원> ();
arraylist.add(new Employe("mateo", 32, 20000));
arraylist.add(new Employe("katia", 26, 10000));
arraylist.add(new Employe("aline", 30, 40000));
arraylist.add(new Employe("살림", 28, 35000));
//이름으로 정렬
Collections.sort(arraylist, Employee.ComparatorName);
System.out.println("이름별로 정렬된 직원 목록:");
for(직원 e:arraylist)
System.out.println(e);
//나이별 정렬
Collections.sort(arraylist, Employee.ComparatorAge);
System.out.println("연령별로 정렬된 직원 목록:");
for(직원 e:arraylist)
System.out.println(e);
//급여별 정렬
Collections.sort(arraylist, Employee.ComparatorSalary);
System.out.println("급여별로 정렬된 직원 목록:");
for(직원 e:arraylist)
System.out.println(e);
}
}
이름별로 정렬된 직원 목록:
[이름=aline, 나이=30, 급여=40000]
[이름=katia, 나이=26, 급여=10000]
[이름=마테오, 나이=32, 급여=20000]
[이름=살림, 나이=28, 급여=35000]
연령별 직원 목록:
[이름=카티아, 나이=26, 급여=10000]
[이름=살림, 나이=28, 급여=35000]
[이름=알린, 나이=30, 급여=40000]
[이름=마테오, 나이=32, salary=20000]
급여별 직원 목록:
[name=katia, age=26, salary=10000]
[name=mateo, age=32, salary=20000]
[name=salim, age=28, salary=35000]
[name=aline, age=30, salary=40000]
Please disable your ad blocker and refresh the window to use this website.