2013-04-26 4 views
0

알 수없는 데이터 형식을 int과 비교할 수 있습니까? 최대 노드를 가져 오는 함수를 작성하려고하지만 데이터 유형이 E가 아닌 int입니다.comapre "알 수없는"값이있는 데이터

내 코드 지금까지 IS ..

public E getMax() { 
    if (isEmpty()) { 
    throw new NoSuchElementException(" error ") ; 
    } else { 
    Node n = first; 

    E x ; 
    int max = 0 ; 
    while (n!=null) { 
     if (n.data > x) { 
     max = n.data; 
     } 
    } 
    return x; 
    } 
} 
+1

을'E ​​Comparable가 를 확장'다음'x.compareTo를 사용 (...)'를 사용하여 최대 값을 찾습니다. – Supericy

답변

2

은 아마 이런 걸 (I 그 n.data 있으리라 믿고있어 타입 E입니다)을 할 것입니다.

일반에 대한

, 내가 가진 것 :

class YourClass<E extends Comparable<? super E>> 

다음 getMax 방법과 같이 보일 것이다 : 그냥이

public E getMax() 
{ 
    if (isEmpty()) 
     throw new NoSuchElementException(" error "); 

    Node n = first; 

    E max = n.data; 

    while (n != null) 
    { 
     if (n.data.compareTo(max) > 0) // if n.data > max 
      max = n.data; 

     n = n.next; // move to the next node 
    } 

    return max; 
} 
+1

+1. 'max = n'을'max = n.data' (두 위치)로 변경하고 'Comparable '을'Comparable '으로 변경하고 싶을 수도 있습니다. –

+0

@Lonenebula 좋은 눈, 고정해야합니다. 추신 : 당신이 모르는 경우, 다른 사람들의 질문/답변을 편집 할 수 있습니다! – Supericy

+1

비교 대상 과 비교 대상 의 차이점은 무엇입니까? –

관련 문제