Bag作为数组在Java中的实现

Bag作为数组在Java中的实现,java,collections,bag,Java,Collections,Bag,我应该实现一个bag数据结构(也称为multiset),一个无序的 项目的同质值(任何Java对象,不包括null)的集合,这些值可能具有重复项。我在互联网上做了大量的搜索,但很难集中精力使用数组而不是列表之类的东西,并且不太理解在类中使用数组的语法 我需要实现所有java.util.Collection,除非抛出一个UnsupportedOperationException。是的,我必须使用阵列,当我添加阵列时,容量必须增加10。我的问题是,我不确定如何处理contains方法、clear方法

我应该实现一个bag数据结构(也称为multiset),一个无序的 项目的同质值(任何Java对象,不包括null)的集合,这些值可能具有重复项。我在互联网上做了大量的搜索,但很难集中精力使用数组而不是列表之类的东西,并且不太理解在类中使用数组的语法

我需要实现所有java.util.Collection,除非抛出一个UnsupportedOperationException。是的,我必须使用阵列,当我添加阵列时,容量必须增加10。我的问题是,我不确定如何处理contains方法、clear方法、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; 
    }
}
import java.util.Collection;
导入java.util.Iterator;
类包实现收集{
私有T[]数组;
公共尺寸;
公文包(){
数组=(T[])新对象[10];
}
公共包(其他){
//不知道该在这里放什么
//创建一个包,其中包含作为集合传递给它的所有项目
}
公共整数大小(){
返回袋大小;
}
公共布尔值为空(){
如果(size()=0)
返回true;
其他的
返回false;
}
公共布尔包含(对象o){
//不知道该在这里放什么
/*如果此集合包含指定的元素,则返回true。更正式地说,
当且仅当此集合至少包含一个元素e时返回true
(o==null?e==null:o.equals(e))*/
return(o.toArray()==null?this.toArray()==null:o.toArray()==this.toArray());
}
}
公共迭代器迭代器(){
抛出新的UnsupportedOperationException(“未实现”);
}
公共对象[]toArray(){
返回数组;
}
公共T[]toArray(T[]a){
抛出新的UnsupportedOperationException(“未实现”);
}
公共布尔加法(TE){
if(行李大小>=数组长度)
返回false;
其他的
{
保证容量(袋大小+10);
数组[bagSize]=e;
bagSize++;
返回true;
}
}
公共布尔删除(对象o){
对于(int i=0;i c){
抛出新的UnsupportedOperationException(“未实现”);
}
公共布尔保留(集合c){
抛出新的UnsupportedOperationException(“未实现”);
}
公共空间清除(){
//不知道该在这里放什么
/*从此集合中删除所有元素(可选操作)
此调用返回后集合将为空(除非它引发异常)*/
}
@凌驾
公共int hashCode(){
抛出新的UnsupportedOperationException(“未实现”);
}
@凌驾
公共布尔等于(对象e){
如果(e==null){
返回false;
}
如果(getClass()!=e.getClass()){
返回false;
}
最终行李其他=(行李)e;
返回true;
}
公共空间容量(国际最小容量){
T[]比格拉雷;

如果(array.length,您可以使用guava的Multiset实现作为参考。这会给您一些想法
我对
中包含的内容感到困惑,
中包含的内容
…您正在调用
toArray()
对象,该对象没有
toArray()
方法。这表明对您试图执行的操作存在一些根本性的误解。尽管如此,您实际上似乎知道如何检查集合是否包含给定对象,因为您必须找到该对象才能
删除它。您的
删除
方法返回的
布尔值与如果用同一个对象调用t
contains
,那么它就有了。我想您可以从中进行操作

(顺便说一句,
remove
方法有一个可能导致内存泄漏的错误……当它将数组中的对象向左移动1时,它不会将不再包含在集合中的数组插槽设置为
null

addAll
非常简单……您得到了所有需要添加的元素的
集合,并且您有一个可以添加元素的
add
方法。这些方法结合在一起。(
addAll
也是您实现第二个构造函数真正需要的。)

clear
也很简单。调用它后,数组需要没有对任何对象的引用,并且包的大小需要为0。请考虑一下如何做到这一点

iterator()
的工作实现将对您有很大帮助,因为许多
Collection
方法(包括
clear
)可以通过使用集合的
iterator
(方便的抽象类
AbstractCollection
来实现),但实现它比仅仅实现一个可能不使用它的基本
clear
要困难一些

还有一张小纸条

public boolean isEmpty() {
    if(size()==0)
        return true;
    else
        return false;
}
最好写为:

public boolean isEmpty() {
  return size() == 0;
}

因为
size()==0
已经是一个
boolean
表达式,
if
/
else
是多余的。

看看有什么困难吗?你不知道如何迭代包来搜索特定的元素吗?是的,这是其中的一部分…我不知道如何迭代其他对象数组,例如,包含m的数组方法。@user481211:我想你一定是误解了
包含
方法的功能。它被赋予了一个
对象
(可以是任何对象,包括
null
),并检查这个集合(你包的这个实例)是否正确包含该对象。抱歉,这对我没有帮助,因为我已经理解了每个方法应该做什么。更新了链接。你可以找到实现multisetI的类。我不认为Guava的任何multisets都是直接在