一、返回值
二、克隆
三、通用包实现
package jiangning.collection;
public class ArrayBag<E> implements Cloneable {
private E[] data ;
private int manyItems ;
/**
* <p>Title: 构造器</p>
* <p>Description: 初始容量为10 </p>
*/
public ArrayBag(){
final int INITIAL_CAPACITY = 10;//初始容量10
manyItems = 0;
data = (E[])new Object[INITIAL_CAPACITY] ;//定义数组,强制类型转换,
}
/**
* <p>Title:根据传入的参数确定创建数组容量大小 </p>
* <p>Description: 带参数构造器 </p>
* @param initalCapacity 容量大小
*/
public ArrayBag(int initalCapacity){
if(initalCapacity < 0){
throw new IllegalArgumentException("The initalCapacity is negative " + initalCapacity);
}
data = (E[])new Object[initalCapacity] ;
manyItems = 0;
}
/**
* @Title: add
* @Description: 向数组中添加一个元素
* @param element
* @author jiangNing
*/
public void add(E element){
if(manyItems == data.length){
ensureCapacity(( manyItems + 1 ) * 2);//将容量扩大为原来一倍
}
data[manyItems ] = element;
manyItems++;
}
/**
* @Title: add
* @Description: 根据可变参数向数组中添加元素
* @param elements
* @author jiangNing
*/
public void add(E ... elements){
if((manyItems + elements.length) > data.length ){
ensureCapacity(( manyItems + elements.length ) * 2);
}
System. arraycopy(elements, 0, data, manyItems, elements.length);
manyItems += elements.length ;
}
/**
* @Title: addAll
* @Description: 将另外一个包的元素添加到当前包中
* @param addend 要添加的包
* @author jiangNing
*/
public void addAll(ArrayBag<E> addend){
ensureCapacity( manyItems + addend.manyItems );
System. arraycopy(addend, 0, data, manyItems, addend.manyItems);
manyItems += addend.manyItems ;
}
//???不理解,克隆是如何实现的。
public ArrayBag<E> clone(){
ArrayBag<E> answer;
try {
answer = (ArrayBag<E>)super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException("This class does not implement Cloneable.");
}
answer. data = data .clone();
return answer;
}
/**
* @Title: countOccurrences
* @Description: 统计给定元素出现的次数
* @param target 要查找的元素
* @return
* @author jiangNing
*/
public int countOccurrences(E target){
int answer;//进行统计
int index;//数组下标进行遍历
answer = 0;
for(index = 0; index < manyItems ; index++){
if(target == data [index]){
answer++;
}
}
return answer;
}
/**
* @Title: ensureCapacity
* @Description: 将数组容量扩充为原来的一倍
* @param minimumCapacity
* @author jiangNing
*/
public void ensureCapacity(int minimumCapacity){
E[] biggerArray;
if(data .length < minimumCapacity){//判断容量大小
biggerArray = (E[]) new Object[minimumCapacity] ;//定义新的数组
System. arraycopy(data, 0, biggerArray, 0, manyItems);//将原来的数组复制到新的数组中
data = biggerArray;//将新的数组赋值给原来的数组变量。
}
}
/**
* @Title: getCapacity
* @Description: 获取当前数组的容量
* @return
* @author jiangNing
*/
public int getCapacity(){
return data .length ;
}
/**
* @Title: grab
* @Description: 随机的获取数组中的一个元素
* @return
* @author jiangNing
*/
public E grab(){
int i;
if(manyItems == 0){
throw new IllegalStateException("Bag size is zero");
}
i = ( int)(Math.random() * manyItems) + 1;
//Math.random()产生0到1之间的小数,包括0,但是不包括1
return data [i];
}
/**
* @Title: remove
* @Description: 删除数组中给定的元素
* @param element
* @return
* @author jiangNing
*/
public boolean remove(E target){
int index;
if(target == null){
index = 0;
while((index < manyItems ) && (data[index] != null)){
index ++;
}
} else{
index = 0;
while((index < manyItems ) && (!target.equals(data[index]))){
index ++;
}
}
if(index == manyItems ){//没有找到指定元素
return false ;
} else{//找到指定元素
manyItems--;//元素总数减去1
data[index] = data [manyItems ];//将最后一个元素赋值给要删除的元素
data[manyItems ] = null; //将最后一个元素设置为null
return true ;
}
}
/**
* @Title: size
* @Description: 在包中元素个数
* @return
* @author jiangNing
*/
public int size(){
return manyItems ;
}
/**
* @Title: trimToSize
* @Description: 减少包的当前容量为实际长度
* @author jiangNing
*/
public void trimToSize(){
E[] trimmedArray;
if(data .length != manyItems){
trimmedArray = (E[]) new Object[manyItems] ;
System. arraycopy(data, 0, trimmedArray, 0, manyItems);
data = trimmedArray;
}
}
/**
* @Title: union
* @Description: 将两个包合并为一个包
* @param <E>
* @param b1
* @param b2
* @return
* @author jiangNing
*/
public static <E> ArrayBag<E> union(ArrayBag<E> b1, ArrayBag<E> b2){
//定义一个新的包,容量为两个包的和
ArrayBag<E> answer = new ArrayBag<E>(b1.getCapacity() + b2.getCapacity());
//将两个包复制到新的包中
System. arraycopy(b1.data, 0, answer. data, 0, b1.manyItems );
System. arraycopy(b2.data, 0, answer. data, b1.manyItems , b2.manyItems);
//返回新的包
answer. manyItems = b1.manyItems + b2.manyItems;
return answer;
}
public E[] getData() {
return data ;
}
public void setData(E[] data) {
this.data = data;
}
public int getManyItems() {
return manyItems ;
}
public void setManyItems(int manyItems) {
this.manyItems = manyItems;
}
}