2016-08-08 2 views
0

Beacon 클래스 (AltBeacon) 프레임 워크에 대한 사용자 지정 비교기를 구현하려고합니다.사용자 지정 비교기 구현하기

 Collections.sort(Arrays.asList(beacons), new Comparator<Beacon>(){ 
     @Override 
     public int compare(Beacon lhs, Beacon rhs) { 
      double diff = lhs.getDistance() - rhs.getDistance(); 

      if (diff > 0){ 
       return 1; 
      } 

      if (diff < 0){ 
       return -1; 
      } 


      return 0; 
     } 
    }); 

그것은 다음과 같은 오류와 함께 실패합니다

Error:(176, 19) error: no suitable method found for sort(List<Collection<Beacon>>,<anonymous Comparator<Beacon>>) method Collections.<T#1>sort(List<T#1>) is not applicable (cannot infer type-variable(s) T#1 (actual and formal argument lists differ in length)) method Collections.<T#2>sort(List<T#2>,Comparator<? super T#2>) is not applicable (inferred type does not conform to upper bound(s) inferred: Collection<Beacon> upper bound(s): Beacon,Object) where T#1,T#2 are type-variables: T#1 extends Comparable<? super T#1> declared in method <T#1>sort(List<T#1>) T#2 extends Object declared in method <T#2>sort(List<T#2>,Comparator<? super T#2>)

+0

Arrays.asList (비콘) 호출에 유형을 추가하는 데 도움이 될 수 있습니다. http://stackoverflow.com/questions/15799359/is-there-a-way-to-force-the-return-type-of -arrays-aslist –

+0

'비컨 '이 이미'Collection' 객체 인 것처럼 보이므로 왜 그것을'List'에 넣을까요? –

답변

1

Collections.sort() 종류의 목록 인플레 이스를.

당신이하고있는 일은 새로운 목록을 전달하는 것입니다.하지만 통화 후에는 더 이상 사용할 수 없으므로 쓸모가 없습니다.

먼저 목록에 비컨을 넣은 다음 정렬 한 다음 정렬 된 목록을 사용하십시오.

관련 문제