package com.arraylist.bottomimplementation;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* ArrayList底层源码实现
* @author zhuaisha98kbaoni
*
*/
public class ArrayListBottomImplementationTest<AnyType> implements Iterable<AnyType> {
public static final int INITIAL_CAPACITY = 10; //最初数组的长度
private int size; //记录集合中元素的个数
public AnyType[] objects; //集合用来放置数组的对象
@SuppressWarnings("unchecked")
public ArrayListBottomImplementationTest() {
objects =(AnyType[]) new Object[INITIAL_CAPACITY];
}
/*
* 集合中的元素个数
*/
public int size(){
return size;
}
/*
* 集合是否为空
*/
public boolean isEmpty(){
return size == 0 ? true : false;
}
/*
* 判断索引值是否越界
*/
public void indexNum(int index){
if(index < 0 || index > size() - 1){
throw new RuntimeException("输入的索引不正确!");
}
}
/*
* 根据索引取值
*/
public AnyType get(int index){
indexNum(index);
return objects[index];
}
/*
* 用指定的元素替换此列表中指定位置的元素。
* 结果
*该元素以前在指定的位置
*/
public AnyType set(int index,AnyType anyType){
indexNum(index);
AnyType anyType2 = objects[index];
objects[index] = anyType;
return anyType2;
}
/*
* 扩容算法
*/
public void ensureCapacity(int capacity){
@SuppressWarnings("unchecked")
AnyType[] anyType =(AnyType[]) new Object[INITIAL_CAPACITY + capacity];
for (int i = 0; i < anyType.length; i++) {
anyType[i] = objects[i];
}
objects = anyType;
}
/*
* 加入值---末尾加入
* 将指定的元素追加到此列表的末尾。
*/
public boolean add(AnyType anyType){
add(size(),anyType);
size++;
return true;
}
/*
* 在此列表中的指定位置插入指定的元素
*/
public void add(int index,AnyType anyType){
if(index < 0 || index > size){
throw new RuntimeException("索引错误!");
}
//是否要扩容
if(size() == objects.length){
ensureCapacity(INITIAL_CAPACITY / 2);
}
System.arraycopy(objects, index, objects, index + 1 , objects.length - index - 1);
objects[index] = anyType;
size++;
}
/*
* 删除某个索引所对应的值
* 删除该列表中指定位置的元素。
*/
public AnyType remove(int index){
AnyType anyType = objects[index];
System.arraycopy(objects, index + 1, objects, index, objects.length - index - 1);
size--;
return anyType;
}
/*
* 重写迭代器
* @see java.lang.Iterable#iterator()
*/
@Override
public Iterator<AnyType> iterator() {
return new ArrayListIterator();
}
private class ArrayListIterator implements Iterator<AnyType>{
private int current;
@Override
public boolean hasNext() {
return current < size() ;
}
@Override
public AnyType next() {
if (!hasNext()) {
throw new NoSuchElementException("没有这样的元素!");
}
return objects[current++];
}
@Override
public void remove() {
ArrayListBottomImplementationTest.this.remove(--current);
}
}
public static void main(String[] args) {
ArrayListBottomImplementationTest<String> array = new ArrayListBottomImplementationTest<>();
//加入值
array.add(0, "zhangsan");
array.add(1, "里斯");
array.add(2, "赵六");
array.add(3, "王五");
array.add(4, "七牛");
array.add(5, "妮妮");
//集合是否为空
System.out.println("集合是否为空:" + array.isEmpty());
//元素个数
System.out.println("元素个数" + array.size());
//根据索引取值
System.out.println("根据索引取到的值为:" + array.get(4));
//System.out.println("根据索引取到的值为:" + array.get(6));
//根据提供的索引和值替换原先的值
String string = array.set(4, "dfjkdf");
System.out.println("替换的原先值为:" + string);
System.out.println("原先位置的值现在为:" + array.get(4));
//末尾加值
boolean b = array.add("你是一个笨蛋");
if(b){
System.out.println("加入成功!");
}else{
throw new RuntimeException("加入不成功!");
}
//删除指定位置的值
System.out.println("删除的索引对应的值为:" + array.remove(4));
//迭代
Iterator<String> iterator = array.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next() + "\t");
}
}
}