문제

To find the minimum value in a collection of given types, what would I need to set "value" to in order to compare it to "min"? Value should be the next element in the collection and it should iterate until the collection is fully read through.

public <T> T min(Collection<T> c, Comparator<T> comp) {
if ((c == null) || (comp == null)) {
     throw new IllegalArgumentException();
  }

  if (c.isEmpty() == true) {
     throw new NoSuchElementException();
  }

  Iterator itr = c.iterator(); 
  T min = (T)itr.next();
  T value = ;
  while (itr.hasNext()) {   
     if (comp.compare(min, value) < 0) { 
        min = value;  
     }
  }
  return min;

}

도움이 되었습니까?

해결책

Use the following code:

 Iterator itr = c.iterator(); 
 T min = (T)itr.next();
 T value;
 while (itr.hasNext()) { 
      value=(T)itr.next();
      if (comp.compare(min, value) < 0) { 
         min = value;  
      }
 }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top