我應該爲一個項目實現一個bag數據結構(也稱爲multiset),一個無序的 齊次值集合(任何Java對象,不包括null)。我在互聯網上進行了大量的搜索,但是我很難用數組代替List之類的東西,也不太瞭解在類中使用數組的語法。作爲Java中的數組實現Bag實現
我需要實現所有的java.util.Collection,除非拋出一個UnsupportedOperationException異常。是的,我必須使用一個數組,當我添加到它時,容量必須增加10.我的問題是,我不知道如何處理包含方法,明確方法, addAll方法,第二個構造函數。希望我添加的其他東西也能順利運行。我已經在註釋塊中包含了API定義。任何輸入都可以幫助我。
正如Mark在下面問到的,我不明白如何通過包搜索特定元素。
import java.util.Collection;
import java.util.Iterator;
class Bag<T> implements Collection<T>{
private T[] array;
public int bagSize;
public Bag(){
array=(T[])new Object[10];
}
public Bag(Collection<T> other){
//Not sure what to put here
//creates a bag containing all of the items passed to it as a Collection<T>
}
public int size() {
return bagSize;
}
public boolean isEmpty() {
if(size()==0)
return true;
else
return false;
}
public boolean contains(Object o) {
//Not sure what to put here
/*Returns true if this collection contains the specified element. More formally,
returns true if and only if this collection contains at least one element e such
that (o==null ? e==null : o.equals(e)). */
return (o.toArray()==null ? this.toArray()==null : o.toArray() == this.toArray());
}
}
public Iterator<T> iterator() {
throw new UnsupportedOperationException("not implemented.");
}
public Object[] toArray() {
return array;
}
public <T> T[] toArray(T[] a) {
throw new UnsupportedOperationException("not implemented.");
}
public boolean add(T e) {
if(bagSize>=array.length)
return false;
else
{
ensureCapacity(bagSize+10);
array[bagSize]=e;
bagSize++;
return true;
}
}
public boolean remove(Object o) {
for(int i=0; i<bagSize; i++)
if(array[i].equals(o)){
for(int j=i; j<bagSize-1; j++)
array[j]=array[j+1];
bagSize--;
return true;
}
return false;
}
public boolean containsAll(Collection<?> c) {
throw new UnsupportedOperationException("not implemented.");
}
public boolean addAll(Collection<? extends T> c) {
//Not sure what to put here
/*Adds all of the elements in the specified collection to this collection
(optional operation). The behavior of this operation is undefined if the specified
collection is modified while the operation is in progress. (This implies that the
behavior of this call is undefined if the specified collection is this collection,
and this collection is nonempty.) */
}
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException("not implemented.");
}
public boolean retainAll(Collection<?> c) {
throw new UnsupportedOperationException("not implemented.");
}
public void clear() {
//Not sure what to put here
/*Removes all of the elements from this collection (optional operation). The
collection will be empty after this call returns (unless it throws an exception).*/
}
@Override
public int hashCode(){
throw new UnsupportedOperationException("not implemented.");
}
@Override
public boolean equals(Object e) {
if (e == null) {
return false;
}
if (getClass() != e.getClass()) {
return false;
}
final Bag<T> other = (Bag<T>) e;
return true;
}
public void ensureCapacity(int minCapacity){
T[] biggerArray;
if(array.length<minCapacity){
biggerArray=(T[]) new Object[minCapacity];
System.arraycopy(array, 0, biggerArray, 0, bagSize);
array=biggerArray;
}
}
另請參閱http://stackoverflow.com/questions/4083495/bag-data-structure-multiset-class-implemenation-in-java-using-fixed-array – 2010-11-04 01:49:31
有什麼困難?你不知道如何遍歷你的包來搜索特定的元素? – 2010-11-04 01:55:50
是的,這是它的一部分......我不知道如何遍歷其他對象數組,例如,包含方法。 – user481211 2010-11-04 01:57:29