没写什么注释,想看的直接拷贝吧,复习下数据结构
[quote]
package com.javaeye.upload;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class MyList<T> implements List<T> {
private T[] array;
private int size = 0;
private static final int INIT_SIZE = 20;
public MyList() {
this(INIT_SIZE);
}
public MyList(int size) {
array = (T[]) new Object[size];
}
public boolean isFull() {
return array.length == size;
}
public boolean add(T e) {
if (!isFull()) {
array[size] = e;
size++;
return true;
}
return false;
}
public void add(int index, T element) {
if (index < 0 || index > array.length) {
throw new IndexOutOfBoundsException("数组越界");
}
if (array.length == size) {
makeRoom();
}
for (int j = size; j > index; j--) {
array[j] = array[j - 1];
}
array[index] = element;
size++;
}
private void makeRoom() {
T[] a = (T[]) new Object[array.length * 2];
for (int i = 0; i < array.length; i++) {
a[i] = array[i];
}
array = a;
}
public boolean addAll(Collection<? extends T> c) {
if (c.size() < array.length - size) {
makeRoom();
}
T[] cToArray = (T[]) c.toArray();
// 第一种方法
// for (int i = 0; i < cToArray.length; i++) {
// array[size + i] = cToArray[i];
// size++;
// }
// if (size > array.length) {
// return false;
// }
// 第二种方法,
System.arraycopy(cToArray, 0, array, size, cToArray.length);
size = size + c.size();
return true;
}
public boolean addAll(int index, Collection<? extends T> c) {
if (c.size() < array.length - size) {
makeRoom();
}
Object[] cToArray = c.toArray();
for (int i = 0; i < c.size(); i++) {
array[size + i - 1] = array[size + i];
}
if (size > array.length) {
return false;
}
return true;
}
public void clear() {
for (int i = 0; i < size; i++) {
array[i] = null;
}
size = 0;
}
public boolean contains(Object o) {
for (int i = 0; i < size; i++) {
if (array[i].equals(o)) {
return true;
}
}
return false;
}
public boolean containsAll(Collection<?> c) {
boolean flag = false;
T[] a = (T[]) c.toArray();
for (int i = 0; i < c.size(); i++) {
if (!contains(a[i])) {
return false;
}
}
return true;
}
public T get(int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("数组越界");
}
return array[index];
}
public int indexOf(Object o) {
for (int i = 0; i < size; i++) {
if (array[i].equals(o)) {
return i;
}
}
throw new RuntimeException("元素未找到");
}
public boolean isEmpty() {
return size == 0;
}
public Iterator<T> iterator() {
return null;
}
public int lastIndexOf(Object o) {
for (int i = size - 1; i >= 0; i--) {
if (array[i].equals(o)) {
return i;
}
}
throw new RuntimeException("元素未找到");
}
public ListIterator<T> listIterator() {
return null;
}
public ListIterator<T> listIterator(int index) {
// TODO Auto-generated method stub
return null;
}
public boolean remove(Object o) {
int i = indexOf(o);
remove(i);
return false;
}
public T remove(int index) {
T t = array[index];
for (int j = index; j < size - 1; j++) {
array[j] = array[j + 1];
}
array[--size] = null;
return t;
}
public T set(int index, T element) {
T o = array[index];
array[index] = element;
return o;
}
public int size() {
return size;
}
public List<T> subList(int fromIndex, int toIndex) {
List<T> list = null;
for (int i = fromIndex; i < toIndex; i++) {
list.add(array[i]);
}
return list;
}
public Object[] toArray() {
return (Object[]) array;
}
public <T> T[] toArray(T[] a) {
// TODO Auto-generated method stub
return null;
}
public boolean removeAll(Collection<?> c) {
boolean flag = false;
T[] o = (T[]) c.toArray();
for (int i = 0; i < o.length; i++) {
if (contains(o[i])) {
remove(o[i]);
flag = true;
}
}
return flag;
}
public boolean retainAll(Collection<?> c) {
boolean flag = false;
T[] o = (T[]) c.toArray();
for (int i = 0; i < o.length; i++) {
if (!contains(o[i])) {
remove(o[i]);
flag = true;
}
}
return flag;
}
public static void main(String[] args) {
MyList list = new MyList();
list.add(new Integer(1));
list.add(new Integer(2));
list.add(new Integer(3));
// System.out.println(list.indexOf(3));
// System.out.println(list.size());
List test = new ArrayList();
test.add(1);
test.add(2);
// System.out.println(list.addAll(test));
// System.out.println(list.size);
// list.add(2, new Integer(7));
// System.out.println(list.indexOf(3));
// System.out.println(list.contains(7));
// System.out.println(list.containsAll(test));
// System.out.println(list.get(2));
// System.out.println(list.lastIndexOf(2));
System.out.println(list.removeAll(test));
System.out.println(list.get(0));
}
}
[/quote]
[quote]
package com.javaeye.upload;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class MyList<T> implements List<T> {
private T[] array;
private int size = 0;
private static final int INIT_SIZE = 20;
public MyList() {
this(INIT_SIZE);
}
public MyList(int size) {
array = (T[]) new Object[size];
}
public boolean isFull() {
return array.length == size;
}
public boolean add(T e) {
if (!isFull()) {
array[size] = e;
size++;
return true;
}
return false;
}
public void add(int index, T element) {
if (index < 0 || index > array.length) {
throw new IndexOutOfBoundsException("数组越界");
}
if (array.length == size) {
makeRoom();
}
for (int j = size; j > index; j--) {
array[j] = array[j - 1];
}
array[index] = element;
size++;
}
private void makeRoom() {
T[] a = (T[]) new Object[array.length * 2];
for (int i = 0; i < array.length; i++) {
a[i] = array[i];
}
array = a;
}
public boolean addAll(Collection<? extends T> c) {
if (c.size() < array.length - size) {
makeRoom();
}
T[] cToArray = (T[]) c.toArray();
// 第一种方法
// for (int i = 0; i < cToArray.length; i++) {
// array[size + i] = cToArray[i];
// size++;
// }
// if (size > array.length) {
// return false;
// }
// 第二种方法,
System.arraycopy(cToArray, 0, array, size, cToArray.length);
size = size + c.size();
return true;
}
public boolean addAll(int index, Collection<? extends T> c) {
if (c.size() < array.length - size) {
makeRoom();
}
Object[] cToArray = c.toArray();
for (int i = 0; i < c.size(); i++) {
array[size + i - 1] = array[size + i];
}
if (size > array.length) {
return false;
}
return true;
}
public void clear() {
for (int i = 0; i < size; i++) {
array[i] = null;
}
size = 0;
}
public boolean contains(Object o) {
for (int i = 0; i < size; i++) {
if (array[i].equals(o)) {
return true;
}
}
return false;
}
public boolean containsAll(Collection<?> c) {
boolean flag = false;
T[] a = (T[]) c.toArray();
for (int i = 0; i < c.size(); i++) {
if (!contains(a[i])) {
return false;
}
}
return true;
}
public T get(int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("数组越界");
}
return array[index];
}
public int indexOf(Object o) {
for (int i = 0; i < size; i++) {
if (array[i].equals(o)) {
return i;
}
}
throw new RuntimeException("元素未找到");
}
public boolean isEmpty() {
return size == 0;
}
public Iterator<T> iterator() {
return null;
}
public int lastIndexOf(Object o) {
for (int i = size - 1; i >= 0; i--) {
if (array[i].equals(o)) {
return i;
}
}
throw new RuntimeException("元素未找到");
}
public ListIterator<T> listIterator() {
return null;
}
public ListIterator<T> listIterator(int index) {
// TODO Auto-generated method stub
return null;
}
public boolean remove(Object o) {
int i = indexOf(o);
remove(i);
return false;
}
public T remove(int index) {
T t = array[index];
for (int j = index; j < size - 1; j++) {
array[j] = array[j + 1];
}
array[--size] = null;
return t;
}
public T set(int index, T element) {
T o = array[index];
array[index] = element;
return o;
}
public int size() {
return size;
}
public List<T> subList(int fromIndex, int toIndex) {
List<T> list = null;
for (int i = fromIndex; i < toIndex; i++) {
list.add(array[i]);
}
return list;
}
public Object[] toArray() {
return (Object[]) array;
}
public <T> T[] toArray(T[] a) {
// TODO Auto-generated method stub
return null;
}
public boolean removeAll(Collection<?> c) {
boolean flag = false;
T[] o = (T[]) c.toArray();
for (int i = 0; i < o.length; i++) {
if (contains(o[i])) {
remove(o[i]);
flag = true;
}
}
return flag;
}
public boolean retainAll(Collection<?> c) {
boolean flag = false;
T[] o = (T[]) c.toArray();
for (int i = 0; i < o.length; i++) {
if (!contains(o[i])) {
remove(o[i]);
flag = true;
}
}
return flag;
}
public static void main(String[] args) {
MyList list = new MyList();
list.add(new Integer(1));
list.add(new Integer(2));
list.add(new Integer(3));
// System.out.println(list.indexOf(3));
// System.out.println(list.size());
List test = new ArrayList();
test.add(1);
test.add(2);
// System.out.println(list.addAll(test));
// System.out.println(list.size);
// list.add(2, new Integer(7));
// System.out.println(list.indexOf(3));
// System.out.println(list.contains(7));
// System.out.println(list.containsAll(test));
// System.out.println(list.get(2));
// System.out.println(list.lastIndexOf(2));
System.out.println(list.removeAll(test));
System.out.println(list.get(0));
}
}
[/quote]