Leonardo Go

Comparable와 Comparator 인터페이스 오버라이딩(2) 본문

Programming/Java

Comparable와 Comparator 인터페이스 오버라이딩(2)

Leonardo Go 2011. 8. 30. 19:15


Comparable인터페이스를 구현하도록 변경해서 이름이 기본 정렬기준이 되도록 만든소스

import java.util.*;

class Student2 implements Comparable<Student2>{
 String name;
 int ban;
 int no;
 int kor, eng, math;

 public Student2(String name, int ban, int no, int kor, int eng, int math) {
  this.name = name;
  this.ban = ban;
  this.no = no;
  this.kor = kor;
  this.eng = eng;
  this.math = math;
 }

 int getTotal() {
  return kor + eng + math;
 }

 float getAverage() {
  return (int) (((getTotal() / 3f) * 10 + 0.5) / 10f);
 }

 public String toString() {
  return name + "," + ban + "," + no + "," + eng + "," + math + ","
    + getTotal() + "," + getAverage();
 }

 @Override
 public int compareTo(Student2 tmp) {
  return name.compareTo(tmp.name);
   
 }

}

public class Exercise11_5 {
 public static void main(String[] args) {
  ArrayList list = new ArrayList();
  list.add(new Student2("홍길동",1,1,100,100,100));
  list.add(new Student2("남궁성",1,2,90,70,80));
  list.add(new Student2("김자바",1,3,80,80,90));
  list.add(new Student2("이자바",1,4,70,90,70));
  list.add(new Student2("안자바",1,5,60,100,80));
  
  Collections.sort(list);
  Iterator it = list.iterator();
  
  while(it.hasNext())
   System.out.println(it.next());
 }
 

}