我应该为一个项目实现一个包数据结构(也称为多重集),一个可能有重复的同类值(任何 Java 对象,不包括 null)的无序集合。我已经在互联网上进行了广泛的搜索,但是很难将我的思想包裹在使用数组而不是 List 之类的东西上,并且不太了解在类中使用数组的语法。
我需要实现所有 java.util.Collection,除非通过抛出 UnsupportedOperationException 进行说明。是的,我必须使用一个数组,当我添加它时,容量必须增加 10。我的问题是我不确定如何处理contains方法、clear方法、addAll方法和第二个构造函数。希望我添加的所有其他内容也能顺利运行。我已将 API 定义包含在注释块中。任何输入都会真正帮助我。
正如马克在下面问的那样,我不明白如何通过包来搜索特定元素。
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;
}
}