本帖最后由 王渠 于 2012-7-26 19:23 编辑
o(∩_∩)o 哈哈,满足你哦,全套的,从接口到抽象类,到实现抽象类,到使用实现后的类的方法,嘿嘿~- public class Exercise25_01 {
- public static void main(String[] args) {
- MyArrayList<String> list1 = new MyArrayList<String>();
- list1.add("Tom");
- list1.add("George");
- list1.add("Peter");
- list1.add("Jean");
- list1.add("Jane");
- MyArrayList<String> list2 = new MyArrayList<String>();
- list2.add("Tom");
- list2.add("George");
- list2.add("Michael");
- list2.add("Michelle");
- list2.add("Daniel");
- list1.addAll(list2);
- System.out.println(list1);
- list1.removeAll(list2);
- System.out.println(list1);
- list1.retainAll(list2);
- System.out.println(list1);
- }
- }
- interface MyList<E> {
- public void add(E e);
- public void add(int index, E e);
- public void clear();
- public boolean contains(E e);
- public E get(int index);
- public int indexOf(E e);
- public boolean isEmpty();
- public int lastIndexOf(E e);
- public boolean remove(E e);
- public E remove(int index);
- public Object set(int index, E e);
- public int size();
- public boolean addAll(MyList<E> otherList);
- public boolean removeAll(MyList<E> otherList);
- public boolean retainAll(MyList<E> otherList);
- }
- abstract class MyAbstrcatList<E> implements MyList<E> {
- protected int size = 0;
- protected MyAbstrcatList() {
- }
- protected MyAbstrcatList(E[] object) {
- for (int i = 0; i < object.length; i++)
- add(object[i]);
- }
- public void add(E e) {
- add(size, e);
- }
- public boolean addAll(MyList<E> otherList) {
- if (otherList != null) {
- addAll(otherList);
- return true;
- } else
- return false;
- }
- public boolean removeAll(MyList<E> otherList) {
- if (otherList != null) {
- removeAll(otherList);
- return true;
- } else
- return false;
- }
- public boolean retainAll(MyList<E> otherList) {
- if (otherList != null) {
- retainAll(otherList);
- return true;
- } else
- return false;
- }
- public boolean isEmpty() {
- return size == 0;
- }
- public int size() {
- return size;
- }
- public boolean remove(E e) {
- if (indexOf(e) >= 0) {
- remove(indexOf(e));
- return true;
- }
- return false;
- }
- }
- class MyArrayList<E> extends MyAbstrcatList<E> {
- public static final int INITIAL_CAPACITY = 16;
- private E[] data = (E[]) new Object[INITIAL_CAPACITY];
- public MyArrayList() {
- }
- public MyArrayList(E[] objects) {
- for (int i = 0; i < objects.length; i++)
- add(objects[i]);
- }
- public void add(int index, E e) {
- ensureCapacity();
- for (int i = size - 1; i >= index; i--)
- data[i] = data[i + 1];
- data[index] = e;
- size++;
- }
- private void ensureCapacity() {
- if (size >= data.length) {
- E[] newData = (E[]) new Object[2 * size + 1];
- System.arraycopy(data, 0, newData, 0, size);
- data = newData;
- }
- }
- public void clear() {
- data = (E[]) new Object[INITIAL_CAPACITY];
- size = 0;
- }
- public boolean contains(E e) {
- for (int i = 0; i < size; i++)
- if (e.equals(data[i]))
- return true;
- return false;
- }
- public E get(int index) {
- return data[index];
- }
- public int indexOf(E e) {
- for (int i = 0; i < size; i++)
- if (e.equals(data[i]))
- return i;
- return -1;
- }
- public int lastIndexOf(E e) {
- for (int i = size - 1; i >= 0; i--)
- if (e.equals(data[i]))
- return i;
- return -1;
- }
- public E remove(int index) {
- E e = data[index];
- for (int i = index; i < size - 1; i++)
- data[i] = data[i + 1];
- data[size - 1] = null;
- size--;
- return e;
- }
- public E set(int index, E e) {
- E old = data[index];
- data[index] = e;
- return old;
- }
- public String toString() {
- StringBuilder result = new StringBuilder("[");
- for (int i = 0; i < size; i++) {
- result.append(data[i]);
- if (i < size - 1)
- result.append(", ");
- }
- return result.toString() + "]";
- }
- public void trimToSize() {
- if (size != data.length) {
- E[] newData = (E[]) new Object[size];
- System.arraycopy(data, 0, newData, 0, size);
- data = newData;
- }
- }
- public boolean removeAll(MyList<E> otherList) {
- for (int i = 0; i < otherList.size(); i++) {
- remove(otherList.get(i));
- }
- for (int i = 0; i < otherList.size(); i++) {
- if (!data.equals(otherList.get(i)))
- return false;
- }
- return true;
- }
- public boolean retainAll(MyList<E> otherList) {
- if (!removeAll(otherList))
- return false;
- if (!removeAll(otherList))
- return false;
- return true;
- }
- public boolean addAll(MyList<E> otherList) {
- for (int i = 0; i < otherList.size(); i++)
- add(otherList.get(i));
- for (int i = 0; i < otherList.size(); i++) {
- if (data.equals(otherList.get(i)))
- return false;
- }
- return true;
- }
- }
复制代码 |