package test;
import java.util.*;
/**
-
Created by Administrator on 2017/3/5/005.
-
实现自定义ArrayList
*/
public class MyArrayList implements Iterable {
//定义默认数组大小
private static final int DEFAULT_CAPACITY = 10;
//定义数组当前大小
private int theSize;
//定义存储的数组
private AnyType[] theItems;//构造函数,实现初始化操作
public MyArrayList() {
doClear();
}public void clear() {
doClear();
}/**
- 实现清空操作:设置数组大小为0,且设置默认大小的数组
*/
public void doClear() {
theSize = 0;
ensureCapacity(DEFAULT_CAPACITY);
}
//获取到ArrayList的长度
public int size() {
return theSize;
}//判断ArrayList是否为空
public boolean isEmpty() {
return size() == 0;
}//提供设置内置数组的大小
public void trimToSize() {
ensureCapacity(size());
}//获取到对应的数据
public AnyType get(int idx) {
if (idx < 0 || idx >= size())
throw new ArrayIndexOutOfBoundsException();
return theItems[idx];
}/**
- 进行更新数据,并返回原来的数据
- @param idx
- @param newVal
- @return
*/
public AnyType set(int idx, AnyType newVal) {
if (idx < 0 || idx >= size())
throw new ArrayIndexOutOfBoundsException();
AnyType old = theItems[idx];
theItems[idx] = newVal;
return old;
}
/**
- 重新构造内置数组,以改变其大小
- @param newCapacity
*/
private void ensureCapacity(int newCapacity) {
if (newCapacity < size())
return;
AnyType[] old = theItems;
theItems = (AnyType[]) new Object[newCapacity];
for (int i = 0; i < size(); i++) {
theItems[i] = old[i];
}
}
public void insert(int idx, AnyType x) {
//theItems.length表示其长度,而theSize表示为具体内容的长度,注意两者区别
//若相等,表示数组要满了,因此要扩充数组
if (theItems.length == size()){
ensureCapacity(size() * 2 + 1);
}
//对数组中的数据进行移动,整体往右移动
for (int i = theSize; i > idx; i --){theItems[i] = theItems[i-1]; } if(idx ==theSize){ theItems[theSize] = x; }else{ for(int i=theSize;i>0;i--){ if(i == idx) break; theItems[i]=theItems[i-1]; } theItems[idx] = x; } //进行赋值,并size++ theItems[idx] = x; theSize ++;
}
//默认方式添加数据
public boolean add(AnyType x) {
add(size(), x);return true;
}
//实现添加数据
public void add(int idx, AnyType x) {
//theItems.length表示其长度,而theSize表示为具体内容的长度,注意两者区别
//若相等,表示数组要满了,因此要扩充数组
if (theItems.length == size()){
ensureCapacity(size() * 2 + 1);
}
//对数组中的数据进行移动,整体往右移动
for (int i = theSize; i > idx; i --){
theItems[i] = theItems[i-1];
}
//进行赋值,并size++
theItems[idx] = x;
theSize ++;
}//添加一个集合
public void addAll(Collection collection){
if (collection == null){
throw new NullPointerException();
}
for (AnyType c : collection){
add©;
}
}
//添加一个集合
public void addAlls(Collection con){
if(con == null){
throw new NullPointerException();
}
for (AnyType list:con) {
add(list);
}
}//实现删除数据,并返回删除的数据
public AnyType remove(int idx){
AnyType removeItem = theItems[idx];
//移动数组位置,整体往左移动
for(int i = idx;i<size()-1;i++){
theItems[i] = theItems[i+1];
}theSize --; return removeItem;
}
//自定义删除,并返回删除的数据
public AnyType rem(int idx){
AnyType old=theItems[idx];
for(int i= idx;i<size()-1;i++){
theItems[i]=theItems[i-1];
}
theSize–;
return old;
}
//实现删除某个元素
public boolean remove(AnyType x){
//先判断是否存在该元素
if (indexOf(x) != -1){
//获取到元素的位置,再进行删除操作
remove(indexOf(x));
return true;
}
return false;
}//是否包含该元素
public boolean contains(AnyType x){
return indexOf(x) != -1;
}//查找元素最早出现的位置
public int indexOf(AnyType x){
int index = 0;
if (x == null){
for (int i = 0; i < theSize; i ++){
if (theItems[i] == null){
return index;
}
index ++;
}
} else {
for (int i = 0; i < theSize; i ++){
if (x.equals(theItems[i])){
return index;
}
index ++;
}
}
return -1;
}//查找元素最后出现的位置
public int lastIndexOf(AnyType x){
int index = theSize-1;
if (x == null){
for (int i = theSize - 1; i >= 0; i --){
if (theItems[i] == null){
return index;
}
index --;
}
} else {
for (int i = theSize - 1; i >= 0; i --){
if (x.equals(theItems[i])){
return index;
}
index --;
}
}
return -1;
}//返回为一个数组
public AnyType[] toArray(){
return theItems;
}public Iterator iterator(){
return new ArrayListIterator();
}private class ArrayListIterator implements Iterator{
//记录当前位置
private int current = 0;//判断是否存在下一项 @Override public boolean hasNext() { return current < size(); } //返回当前位置的元素 @Override public AnyType next() { if (!hasNext()){ throw new NoSuchElementException(); } return theItems[current++]; } //删除当前位置,注意这里是--current,对应上面next的current++ public void remove(){ MyArrayList.this.remove(--current); }
}
- 实现清空操作:设置数组大小为0,且设置默认大小的数组
}