自己实现一个ArrayList
接口
MyList.java
package org.cgz.practice;public interface MyList<E> {public void add(E e);public void add(Integer index,E e);public boolean remove(E e);public E remove(int index);public void clear();public void trimToSize();public E set(int index,E e);public E get(int index);public boolean contain(E e);public int indexOf(E e);public int lastIndexOf(E e);public int size();public boolean isEmpty();public void print();}
?
?
?
MyAbstratList.java
便利类,实现一些共有的操作.
?
package org.cgz.practice;public abstract class MyAbstractList<E> implements MyList<E> {protected int size = 0;@Overridepublic void add(E e) {add(size, e);}@Overridepublic boolean remove(E e) {int index = indexOf(e);if(index>=0) {remove(index);return true;}return false;}@Overridepublic int size() {return size;}@Overridepublic boolean isEmpty() {return size==0;}}
?
MyArrayList.java
package org.cgz.practice;import java.io.Serializable;import java.util.Arrays;import java.util.Collection;public class MyArrayList<E> extends MyAbstractList<E> implements MyList<E>,Cloneable,Serializable{private static final long serialVersionUID = 2100052558420943221L;/**初始数组长度**/private static final int INIT_CAPACITY = 16;/**对象数组**/private transient Object[] data; public MyArrayList(){this.data = new Object[INIT_CAPACITY];}/** * 以指定容量创建数组线性表 * @param initCapacity */public MyArrayList(int initCapacity){if(initCapacity<0) {throw new IllegalArgumentException("Illegal Capacity: "+initCapacity);}data = new Object[initCapacity];}/** * 通过现有集合创建数组线性表 * @param collection */public MyArrayList(Collection<? extends E> collection){data = collection.toArray();size = data.length;//如果返回的不是一个数组对象,那么就使用数组拷贝将data变成Object[]if(data.getClass()!=Object[].class) {data = Arrays.copyOf(data, size, Object[].class);}}/** * 扩充线性表容量 */private void ensureCapacity() {Object[] newData;if(size>=data.length) {newData = new Object[data.length*3/2+1];System.arraycopy(data, 0, newData, 0, size);data = newData;}}/** * 增 */@Overridepublic void add(Integer index, E e) {if(index<0 || index>size) {throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);}ensureCapacity();for(int i=size-1;i>=index;i--) {data[i+1] = data[i];}//System.arraycopy(data, index, data, index+1, size-index);data[index] = e;size++;}/** * 删 */@Overridepublic E remove(int index) {rangCheck(index);E old = (E) data[index];for(int i=index;i<size-1;i++) {data[i] = data[i+1];}data[size-1] = null;size--;return old;}/** * 改 */@Overridepublic E set(int index, E e) {rangCheck(index);E old = (E) data[index];data[index] = e;return old;}/** * 查 */@Overridepublic E get(int index) {rangCheck(index);return (E) data[index];}/** * 判断下标是否越界 * @param index */private void rangCheck(int index) {if(index<0 || index>size-1) {throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);}}/** * 是否包含指定元素 */@Overridepublic boolean contain(E e) {if(indexOf(e)>=0) {return true;}return false;}/** * 返回元素在线性表中最初出现的位置(不存在返回-1) */@Overridepublic int indexOf(E e) {for(int i=0;i<size;i++) {if(data[i].equals(e)) {return i;}}return -1;}/** * 返回元素最后在线性表中出现位置(不存在返回-1) */@Overridepublic int lastIndexOf(E e) {for(int i=size-1;i>=0;i--) {if(data[i].equals(e)) {return i;}}return -1;}/** * 清空线性表 */@Overridepublic void clear() {data = new Object[INIT_CAPACITY];size = 0;}@Overridepublic void trimToSize() {if(size<data.length) {data = Arrays.copyOf(data, size);}}@Overridepublic void print() {System.out.print(this.getClass().getSimpleName()+":[");for(int i=0;i<size;i++) {System.out.print(data[i]+" ");}System.out.print("]");}}
?
?
1 楼 lvwenwen 15 小时前 自己实现一个ArrayList