자바 comparable 구현

자바 comparable 구현

작성일 2020.06.02댓글 1건
    게시물 수정 , 삭제는 로그인 필요

아래의 코드가 Apple코드입니다.

package oop;

public class Apple {
   private long id;
  
   public Apple(long id) {
      this.id = id;
   }
  
   public long getId() {
      return id;
   }
}



Apple 클래스가 Comparable 구현하도록 소스를 수정하시오. 이때 다른 Apple 종류와 비교될 있도록 Comparable<Apple> 구현해야 것이다. 구현을 위해서는 Comparable 선언된 compareTo(…) 메쏘드를 작성해야 것이다. compareTo(…) this(자신) 인자로 넘어온 다른 Apple 객체(that) 사이에서 정렬할 앞에 건지 혹은 뒤에 건지를 결정한 바에 따라 int 값을 반환하도록 되어 있다. 만약 개의 Apple 객체가 순서상 동일한 위치라면 0 반환해야 하고, this 앞선다면 양의 정수값, that 앞선다면 음의 정수값을 반환하도록 코드 작성


Apple코드를 수정하고 나서 다음의 PQApple 클래스에 오류가 생성되지 않게 코드 작성 부탁드립니다!

package oop.day07;

import java.util.PriorityQueue;

import oop.Apple;

public class PQAppleTest {
   public static void main(String[] args) {
      PriorityQueue<Apple> pq = new PriorityQueue<Apple>(5);
     
      pq.add(new Apple(3));
      System.out.println("PriorityQueue = " + pq.toString());
     
      pq.add(new Apple(1));
      System.out.println("PriorityQueue = " + pq.toString());

      pq.add(new Apple(4));
      System.out.println("PriorityQueue = " + pq.toString());

      pq.add(new Apple(2));
      System.out.println("PriorityQueue = " + pq.toString());

      AppleComparator ac = new AppleComparator();
      PriorityQueue<Apple> pq2 = new PriorityQueue<Apple>(5, ac);
     
      pq2.add(new Apple(3));
      System.out.println("PriorityQueue = " + pq2.toString());
     
      pq2.add(new Apple(1));
      System.out.println("PriorityQueue = " + pq2.toString());

      pq2.add(new Apple(4));
      System.out.println("PriorityQueue = " + pq2.toString());

      pq2.add(new Apple(2));
      System.out.println("PriorityQueue = " + pq2.toString());
   }
}


#자바 comparable #자바 comparable 인터페이스 #자바 comparable 사용법 #자바 comparable 정렬 #자바 comparable 오름차순 #자바 comparable 문자열 정렬 #자바 comparable interface #자바 우선순위 큐 comparable #자바 배열 comparable

profile_image 익명 작성일 -

AppleComparator 은 왜 만들었나요? 어떤 클래스인가요??

문제에는 Apple 클래스만 Comparable 인터페이스 구현하라고 되어있는데..

이런식으로 Apple만 바꾸고 ac는 빼버리세요

public class Apple implements Comparable<Apple> { private long id; public Apple(long id) { this.id = id; } public long getId() { return id; } @Override public int compareTo(Apple o) { if (this.id == o.id) { return 0; } else if (this.id < o.id) { return 1; } else { return -1; } } }

자바 comparable

... 정렬하기 위해 컬렉션의 sort를 사용하려면 Comparable이나 Comparator인터페이스를 구현을 하도록 자바 라이브러리가 그렇게 요구를 하고 있기 때문입니다.

자바 comparable 구현

자바 comparable 구현한다는것이 무슨 말입니까 예제 부탁드립니다 [code] public class Animal implements Comparable<Animal>{ public String...

자바 comparable

해외에서 자바 프로그래밍 수업을 듣는 학생입니다. 교수님이 갑자기 인터페이스를... 작업을 Comparable 인터페이스를 구현함으로써 이 구현된 클래스는 서로 비교를 할 수...

자바 comparable 추가질문

... 크고 작다를 비교하는 방법이 두가지 주어지는데요, 하나는 저장되는 객체가 Comparable구현한 경우 compareTo(Member m) 메소드를...

자바 comparable과 comparator...

객체간의 정렬의 기준을 바꾸고 싶을 때 comparable 인터페이스를 구현을 하고 compareTo or compare 메소드를 오버라이딩 해서 자기가 원하는 기준에 맞게 메소드를...

[JAVA} 자바 Comparable과 Comparator...

... 때, Comparable 인터페이스를 확장해서 정렬의 기준을 정의하는 compareTo() 메서드를 구현한다. Comparator : 객체 간의 특정한 정렬이 필요할 때, Comparator...

자바 comparable 인터페이스

안녕하세요 자바를 공부하고있는 학생입니다. 먼저, comarable 인터페이스라고... 감사합니다 Comparable구현해서 compareTo를 정의하고 정렬을 하기 위한 키에 대해서...

java Comparable 인터페이스 구현?????

자바에서 comparable 인터페이스를 꼭 구현 해줘야하나요? 어차피 implements를 붙여서 클래스를 만든다 해도 예를 들어 compareTo 라는 메소드를 다시...