2017-02-10 32 views
1

我正在嘗試找到兩個ResizableArraySet對象的聯合和交集,這兩個對象是使用帶有幾個方法的已修改Set接口創建的。我很難通過兩套和處理兩者。我知道這不是什麼事情發生,但這裏有一些代碼來更好地解釋我正在嘗試做什麼。這是我的測試人員的電話。比較兩個可調整大小的數組集

System.out.println(Arrays.toString((set.union(set2)).toArray())); 

所以set是我在測試器中創建的對象,set2是另一個ResizableArraySet對象,我用它來比較設置。當我打印時,我只是得到一組set2。

這裏是構造函數和實例變量。

public class ResizableArraySet<T> implements Set<T> { 

private int numberOfEntries; 
private int size; 
T[] array; 

/** 
* Constructor for Set without size variable (Default size is 10) 
*/ 
@SuppressWarnings({ "unchecked" }) 
public ResizableArraySet() { 
    array = (T[]) new Object[10]; 
} 

/** 
* Constructor for the Set with size variable 
* 
* @param size 
*/ 
@SuppressWarnings({ "unchecked" }) 
public ResizableArraySet(int size) { 
    this.size = size; 
    array = (T[]) new Object[size]; 
} 

這裏是我的聯合方法的相關代碼。如果交集方法是必要的,我可以在編輯中提供,但我認爲這兩種方法的問題都是一樣的。

@SuppressWarnings({ "unchecked", "unused" }) 
@Override 
public Set<T> union(Set<T> anotherSet) { 
    T[] newArray = (T[]) new Object[anotherSet.getSize()]; 
    int entries = this.size; 
    System.out.println(entries); //Get 0, when the size is not 0 
    for (int x = 0; x < entries; x++) { 
     if (anotherSet.contains(array[x]) == false) { 
      anotherSet.add(array[x]); 
     } 
    } 
    return anotherSet; 
} 

這裏是我的添加,刪除和包含的方法

@Override 
public boolean add(T newEntry) { 
    if (contains(newEntry)) { 
     return false; 
    } else if (array.length > numberOfEntries) { 
     array[numberOfEntries] = newEntry; 
     numberOfEntries++; 
     return true; 
    } else { 
     array = Arrays.copyOf(array, (array.length * 2)); 
     array[numberOfEntries] = newEntry; 
     numberOfEntries++; 
     return true; 
    } 
} 

@Override 
public boolean remove(T anEntry) { 
    if (contains(anEntry) != true) { 
     return false; 
    } 
    for (int x = 0; x < numberOfEntries; x++) { 
     if (array[x].equals(anEntry)) { 
      // Loop to move the values down one index in the main array 
      for (int a = x; a < numberOfEntries; a++) { 
       array[a] = array[a + 1]; 
      } 
      numberOfEntries--; 
     } 
    } 
    return true; 
} 

@Override 
public boolean contains(T anEntry) { 
    for (int x = 0; x < numberOfEntries; x++) { 
     if (array[x].equals(anEntry)) { 
      return true; 
     } 
    } 
    return false; 
} 

所以總結一下我的問題。我無法比較兩個對象,因爲它們都必須使用相同的方法[contains(anEntry),add(anEntry),getSize()]

讓我知道如果我缺少任何有用的代碼。 有沒有人有我的問題的解決方案

+0

'numberOfEntries'在哪裏設置?爲什麼不能調用'this.size()'而不是使用'numberOfEntries'? –

+0

如果'numberOfEntries'的值不正確,那麼將條目添加到集合中並從集合中刪除條目的代碼可能無法正確更新。 –

+0

@DarshanMehta numberOfEntries是構造函數外部的一個實例變量。當我調用this.size和this.getSize()時,我仍然得到相同的錯誤,條目仍然是0.我將編輯這個代碼爲 –

回答

1

以下是你的工會應該是什麼樣子。它使用了一個迭代器,你需要實現它。這是因爲您的otherSet變量的類型爲Set<T>,而不是ResizableArraySet<T>,因此您無法直接訪問其陣列。

public Set<T> union(Set<T> anotherSet) { 
    ResizableArraySet<T> newSet = new ResizableArraySet<>(); 
    for (int i = 0; i < numberOfEntries; i++) { 
     newSet.add(array[i]); 
    } 
    Iterator<T> it = anotherSet.iterator(); 
    while (it.hasNext()) { 
     T el = it.next(); 
     if (!newSet.contains(el)) { 
      newSet.add(el); 
     } 
    } 
    return newSet; 
}