왜 "Unchecked cast from ..."

7 헤 바이킹

Unchecked cast from Object to Compareable<Object>인스턴스가 비교 가능 유형 인 경우에만 입력 된 영역 내부에 있기 때문에를 얻는 이유를 이해할 수 없습니다 . 누군가 나에게 이것을 설명하고 해결책을 줄 수 있습니까, 감사합니다.

내 코드는 다음과 같습니다. 문제는 8 행과 9 행에 있습니다.

public int compare(Object one, Object two) {

if (one instanceof Vector && two instanceof Vector) {
  Vector<? extends Object> vOne = (Vector<?>)one;
  Vector<? extends Object> vTwo = (Vector<?>)two;
  Object oOne = vOne.elementAt(index);
  Object oTwo = vTwo.elementAt(index);

  if (oOne instanceof Comparable && oTwo instanceof Comparable) {
    Comparable<Object> cOne = (Comparable<Object>)oOne;
    Comparable<Object> cTwo = (Comparable<Object>)oTwo;
    if (ascending) {
      return cOne.compareTo(cTwo);
    } else {
      return cTwo.compareTo(cOne);
    }
  }
}
return 1;

}
피터 로리

이 검사되지 않은 경고를 생성 할 때이 개체는 것을 알고하지 않기 ComparableObject. 사실 예를 들어, 그것이 가능성이 매우 가능성이 Integer있다 Comparable<Integer>그러나 이것은 컴파일이 코드를 얻을 수있는 간단한 방법입니다

나는 당신이 사용하는 것이 좋습니다

@SuppressWarning("checked")
Comparable<Object> cOne = (Comparable<Object>)oOne;

또는

Comparable cOne = (Comparable) oOne;

BTW, 당신은 할 수없는 return 1말에 당신은 당신이 경우 다음 사항을 확인 compare(a, b) > 0compare(b, a) < 0

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사