2017-02-08 16 views
0
public class MyArrayList<T> implements MyList<T>{ 
    int num;  //number of things in the list 
    T[] vals;  //to store the contents 

    @SuppressWarnings("unchecked") 
    public MyArrayList() { 
     num = 0; 
     vals = (T[]) new Object[3]; 
    } 

public T getUnique(){ 
     T distinct = null; 
     int count = 0; 
     for (int i=0; i<vals.length; i++){ 
      distinct = vals[i]; 
      for (int j = 0; j<vals.length; j++){ 
       if (vals[j] == vals[i]){ 
        count++; 
       } 
       if (count == 1){ 
        return distinct; 
       } 
      } 
     } 
     if (distinct == null){ 
      throw new IllegalArgumentException(); 
     } 
     return distinct; 
    } 

我想在一個獨特的方法工作。 getUnique方法不接受任何參數,並返回列表中只出現一次的第一個值。 (例如,調用列表[1,2,3,1,2,4]上的方法將返回3,因爲1和 2都顯示不止一次。)如果列表爲空或其所有值都顯示爲多於一次,該方法將引發一個NoSuchElementException嘗試在Java中的ArrayList實現中查找唯一元素。一個getUnique方法

+0

如果方法getUnique不接受任何參數如何通過列表?你在方法裏初始化它? – mirzak

+0

確切的問題是什麼? –

+0

@mirzak看起來這是他的清單實施方法。 Ram:兩個問題:這個作業是什麼,你的問題是什麼? –

回答

1

我增加了一些FIXME對你的代碼:

public T getUnique(){ 
    T distinct = null; 
    int count = 0; // FIXME: move this initialization inside the i loop 
    for (int i=0; i<vals.length; i++){ 
     distinct = vals[i]; 
     for (int j = 0; j<vals.length; j++){ 
      if (vals[j] == vals[i]){ // FIXME: use .equals() not == 
       count++; 
      } 
      if (count == 1){ // FIXME: move this check outside the j loop 
       return distinct; 
      } 
     } 
    } 
    if (distinct == null){ //FIXME: no check needed, just throw it 
     throw new IllegalArgumentException(); 
    } 
    return distinct; //FIXME: no valid return can reach this point 
} 
+0

工作正常。非常感謝。 –

+1

@RamSharma [Here](http://stackoverflow.com/help/someone-answers)是當有人回答你的問題時要做什麼。要將答案標記爲已接受,請點擊答案旁邊的複選標記以將其從灰色變爲空白。您無需在您的問題上添加評論,也不需要在答案中添加「謝謝」。 –

0

帕特里克·帕克的建議將解決您的代碼,但我想提供一個更清潔,更快速地解決發現的問題列表中的唯一元素。此算法運行時間爲O(n)而不是O(n^2)

public static <T> Optional<T> getUnique(List<T> ls) { 
    // Create a map whose keys are elements of the list and whose values are 
    // lists of their occurences. E.g. [1,2,3,1,2,4] becomes {1->[1, 1], 
    // 2->[2, 2], 3->[3], 4->[4]}. Then elements.get(x).size() tells us how 
    // many times x occured in ls. 
    Map<T, List<T>> elements = ls.stream() 
      .collect(Collectors.groupingBy(x -> x)); 
    // Find the first element that occurs exactly one time in ls. 
    return ls.stream().filter(x -> elements.get(x).size() == 1) 
      .findFirst(); 
} 

你可以這樣調用它:

Integer[] vals = {1,2,3,1,2,4}; 
System.out.println(getUnique(Arrays.asList(vals)) 
     .orElseThrow(NoSuchElementException::new)); 

該代碼使用Java 8流和Optional。下面是不使用Java 8語言特性的相同算法的另一個實現;如果你從未遇到過流,你可能會發現它更容易理解。

private static <T> T getUnique(List<T> arr) { 
    Map<T, Integer> numOccurrences = new HashMap<>(); 
    for (T item : arr) { 
     numOccurrences.put(item, 1 + numOccurrences.getOrDefault(item, 0)); 
    } 

    for (T item : arr) { 
     if (numOccurrences.get(item) == 1) { 
      return item; 
     } 
    } 

    throw new NoSuchElementException(); 
} 
相關問題