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方法
如果方法getUnique不接受任何參數如何通過列表?你在方法裏初始化它? – mirzak
確切的問題是什麼? –
@mirzak看起來這是他的清單實施方法。 Ram:兩個問題:這個作業是什麼,你的問題是什麼? –