// File: ArrayBag.java from the package edu.colorado.collections
// Additional javadoc documentation is available from the ArrayBag link in:
// http://www.cs.colorado.edu/~main/docs
package edu.colorado.collections;
/******************************************************************************
* An ArrayBag is a generic collection of references to objects.
* The same object may appear multiple times in a bag.
*
* @note
* (1) The capacity of one of these bags can change after it's created, but
* the maximum capacity is limited by the amount of free memory on the
* machine. The constructor, addItem, clone,
* and union will result in an OutOfMemoryError
* when free memory is exhausted.
* <p>
* (2) A bag's capacity cannot exceed the maximum integer 2,147,483,647
* (Integer.MAX_VALUE). Any attempt to create a larger capacity
* results in a failure due to an arithmetic overflow.
* <p>
* (3) Because of the slow linear algorithms of this
* class, large bags will have poor performance.
*
* @see
* <A HREF="../../../../edu/colorado/collections/ArrayBag.java">
* Java Source Code for this class
* (www.cs.colorado.edu/~main/edu/colorado/collections/ArrayBag.java)
* </A>
*
* @author Michael Main
* <A HREF="mailto:main@colorado.edu"> (main@colorado.edu) </A>
*
* @version
* Jul 5, 2005
*
* @see IntArrayBag
* @see LinkedBag
******************************************************************************/
public class ArrayBag<E> implements Cloneable
{
// Invariant of the ArrayBag class:
// 1. The number of elements in the bag is in the instance variable
// manyItems, which is no more than data.length.
// 2. For an empty bag, we do not care what is stored in any of data;
// for a non-empty bag, the elements in the bag are stored in data[0]
// through data[manyItems-1], and we don't care what's in the
// rest of data.
private E[ ] data;
private int manyItems;
/**
* Initialize an empty bag with an initial capacity of 10. Note that the
* addItem method works efficiently (without needing more
* memory) until this capacity is reached.
* @param - none
* @postcondition
* This bag is empty and has an initial capacity of 10.
* @exception OutOfMemoryError
* Indicates insufficient memory for:
* new Object[10].
**/
public ArrayBag( )
{
final int INITIAL_CAPACITY = 10;
manyItems = 0;
data = (E[]) new Object[INITIAL_CAPACITY];
}
/**
* Initialize an empty bag with a specified initial capacity. Note that the
* addItem method works efficiently (without needing more
* memory) until this capacity is reached.
* @param initialCapacity
* the initial capacity of this bag
* @precondition
* initialCapacity is non-negative.
* @postcondition
* This bag is empty and has the given initial capacity.
* @exception IllegalArgumentException
* Indicates that initialCapacity is negative.
* @exception OutOfMemoryError
* Indicates insufficient memory for: new Object[initialCapacity].
**/
public ArrayBag(int initialCapacity)
{
if (initialCapacity < 0)
throw new IllegalArgumentException
("The initialCapacity is negative: " + initialCapacity);
data = (E[]) new Object[initialCapacity];
manyItems = 0;
}
/**
* Add a new element to this bag. If the new element would take this
* bag beyond its current capacity, then the capacity is increased
* before adding the new element.
* @param element
* the new element that is being inserted
* @postcondition
* A new copy of the element has been added to this bag.
* @exception OutOfMemoryError
* Indicates insufficient memory for increasing the bag's capacity.
* @note
* An attempt to increase the capacity beyond
* Integer.MAX_VALUE will cause the bag to fail with an
* arithmetic overflow.
**/
public void add(E element)
{
if (manyItems == data.length)
{ // Ensure twice as much space as we need.
ensureCapacity((manyItems + 1)*2);
}
data[manyItems] = element;
manyItems++;
}
/**
* Add new elements to this bag. If the new elements would take this
* bag beyond its current capacity, then the capacity is increased
* before adding the new elements.
* @param elements
* (a variable-arity argument)
* one or more new elements that are being inserted
* @postcondition
* A new copy of the element has been added to this bag.
* @exception OutOfMemoryError
* Indicates insufficient memory for increasing the bag's capacity.
* @note
* An attempt to increase the capacity beyond
* Integer.MAX_VALUE will cause the bag to fail with an
* arithmetic overflow.
**/
public void addMany(E... elements)
{
if (manyItems + elements.length > data.length)
{ // Ensure twice as much space as we need.
ensureCapacity((manyItems + elements.length)*2);
}
System.arraycopy(elements, 0, data, manyItems, elements.length);
manyItems += elements.length;
}
/**
* Add the contents of another bag to this bag.
* @param addend
* a bag whose contents will be added to this bag
* @precondition
* The parameter, addend, is not null.
* @postcondition
* The elements from addend have been added to this bag.
* @exception NullPointerException
* Indicates that addend is null.
* @exception OutOfMemoryError
* Indicates insufficient memory to increase the size of the bag.
* @note
* An attempt to increase the capacity beyond
* Integer.MAX_VALUE will cause an arithmetic overflow
* that will cause the bag to fail. Such large collections should use
* a different bag implementation.
**/
public void addAll(ArrayBag<E> addend)
{
// If addend is null, then a NullPointerException is thrown.
// In the case that the total number of items is beyond
// Integer.MAX_VALUE, there will be an arithmetic overflow and
// the bag will fail.
ensureCapacity(manyItems + addend.manyItems);
System.arraycopy(addend.data, 0, data, manyItems, addend.manyItems);
manyItems += addend.manyItems;
}
/**
* Generate a copy of this bag.
* @param - none
* @return
* The return value is a copy of this bag. Subsequent changes to the
* copy will not affect the original, nor vice versa.
* @exception OutOfMemoryError
* Indicates insufficient memory for creating the clone.
**/
public ArrayBag<E> clone( )
{ // Clone an ArrayBag object.
ArrayBag<E> answer;
try
{
answer = (ArrayBag<E>) super.clone( );
}
catch (CloneNotSupportedException e)
{ // This exception should not occur. But if it does, it would probably
// indicate a programming error that made super.clone unavailable.
// The most common error would be forgetting the "Implements Cloneable"
// clause at the start of this class.
throw new RuntimeException
("This class does not implement Cloneable");
}
answer.data = data.clone( );
return answer;
}
/**
* Accessor method to count the number of occurrences of a particular element
* in this bag.
* @param target
* the element that needs to be counted
* @return
* the number of times that target occurs in this bag
**/
public int countOccurrences(E target)
{
int answer;
int index;
answer = 0;
for (index = 0; index < manyItems; index++)
if (target == data[index])
a
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
collections.rar (56个子文件)
collections
BooleanLinkedQueue.java 6KB
ObjectLinkedStack.java 5KB
DoubleLinkedQueue.java 6KB
FloatLinkedStack.java 5KB
LinkedSeq.java 10KB
CharQueue.java 12KB
FloatQueue.java 12KB
ArrayQueue.class 3KB
IntBalancedSet.java 11KB
IntTreeBag.java 5KB
ShortLinkedStack.java 5KB
LinkedBag.class 3KB
IntLinkedQueue.java 6KB
ByteLinkedQueue.java 6KB
ObjectStack.java 9KB
ByteLinkedStack.java 5KB
LinkedQueue.class 2KB
DoubleStack.java 9KB
CharLinkedQueue.java 6KB
DoubleQueue.java 12KB
CharLinkedStack.java 5KB
IntLinkedStack.java 5KB
ArrayQueue.java 11KB
LongLinkedStack.java 5KB
BagWithReceipts.java 7KB
LinkedQueue.java 5KB
DerivedStack.java 3KB
LongStack.java 9KB
ShortLinkedQueue.java 6KB
ChainedTable.java 6KB
DoubleArraySeq.java 13KB
FloatLinkedQueue.java 6KB
BooleanStack.java 9KB
IntQueue.java 11KB
DoubleLinkedSeq.java 8KB
DoubleLinkedStack.java 5KB
ObjectQueue.java 12KB
LongQueue.java 12KB
ObjectLinkedQueue.java 6KB
IntStack.java 9KB
CharStack.java 9KB
IntLinkedBag.java 8KB
ByteQueue.java 12KB
Table.java 8KB
ArrayBag.java 13KB
BooleanLinkedStack.java 5KB
ShortQueue.java 12KB
LinkedBag.java 8KB
BooleanQueue.java 12KB
LinkedStack.java 5KB
ShortStack.java 9KB
IntArrayBag.java 12KB
LongLinkedQueue.java 6KB
ArrayStack.java 9KB
ByteStack.java 9KB
FloatStack.java 9KB
共 56 条
- 1
资源评论
- wzhq13092013-01-22很不错,已经完全实现
- sailing1234562013-12-11不错不错~很多,很全
- spf78084072013-02-18东西的确是很多,很全,但是也太多了。。。应该用不上这么多吧。
- KickEGG2013-06-13新手,拿来学习了,谢谢分享!
sea616
- 粉丝: 0
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功