package Day12;
import java.util.*;
public class MyArrayList implements List {
private Object[] data;
private int count;
public MyArrayList() {
this(10);
}
public MyArrayList(int initCal) {
data = new Object[initCal];
count = 0;
}
public MyArrayList(Collection c) {
data = new Object[c.size()];
for (Object obj : c) {
data[count++] = obj;
}
}
public void add(int arg0, Object arg1) {
if (arg0 > count || arg0 < 0) {
throw new ArrayIndexOutOfBoundsException("参数不合法");
}// System.out.println(3333);
if (count == data.length) {
Object[] obj = new Object[data.length * 2];
//
for (int i = 0; i < data.length; i++) {// //
obj[i] = data[i];// ///////////System.arraycopy(data,0,temp,0,data.length);
}// /////////////////////////
data = obj;
}
for (int i = count; i > arg0; i--) {
data[i] = data[i - 1];
}
data[arg0] = arg1;
count++;
}
public boolean add(Object arg0) {
// if(count==data.length){
// Object[] obj=new Object[data.length*2];
// // obj=data.clone();
// for(int i=0;i<data.length;i++){
// obj[i]=data[i];
// }
// data=obj;
// }
// data[count++]=arg0;
add(count, arg0);
return true;
}
public boolean addAll(Collection arg0) {
// TODO Auto-generated method stub
return false;
}
public boolean addAll(int arg0, Collection arg1) {
// TODO Auto-generated method stub
return false;
}
public void clear() {
data = new Object[10];
count = 0;
System.gc();
}
public boolean contains(Object arg0) {
// for (Object obj : data) {
// ???System.out.println(obj);
for (int i = 0; i < count; i++) {
if (data[i].equals(arg0)) {
return true;
}
}
return false;
}
public boolean containsAll(Collection arg0) {
// TODO Auto-generated method stub
return false;
}
public Object get(int arg0) {
if (arg0 >= count || arg0 < 0) {
throw new ArrayIndexOutOfBoundsException("参数不合法");
}
return data[arg0];
}
public int indexOf(Object arg0) {
for (int i = 0; i < count; i++) {
if (data[i].equals(arg0)) {
return i;
}
}
throw new ArrayIndexOutOfBoundsException("参数不合法");
}
public boolean isEmpty() {
return count == 0;
}
public Iterator iterator() {
return new Iterator() {
int i=0;
public boolean hasNext() {
return i<count;
}
public Object next() {
return data[i++];
}
public void remove() {//把当前找到的元素删掉
MyArrayList.this.remove(--i);//在内部类中调用外部类的属性或方法
}
};
}
public int lastIndexOf(Object arg0) {
// TODO Auto-generated method stub
return 0;
}
public ListIterator listIterator() {
// TODO Auto-generated method stub
return null;
}
public ListIterator listIterator(int arg0) {
// TODO Auto-generated method stub
return null;
}
public Object remove(int arg0) {
if (arg0 >= count || arg0 < 0) {
throw new ArrayIndexOutOfBoundsException("参数不合法");
}
Object temp = data[arg0];
for (int i = arg0; i < count - 1; i++) {
data[i] = data[i + 1];
}
count--;
return temp;
}
public boolean remove(Object arg0) {
try {
int i = this.indexOf(arg0);
remove(i);
return true;
} catch (ArrayIndexOutOfBoundsException e) {
return false;
}
}
public boolean removeAll(Collection arg0) {
// TODO Auto-generated method stub
return false;
}
public boolean retainAll(Collection arg0) {
// TODO Auto-generated method stub
return false;
}
public Object set(int arg0, Object arg1) {
if (arg0 > count || arg0 < 0) {
throw new ArrayIndexOutOfBoundsException("参数不合法");
}
Object temp = data[arg0];
data[arg0] = arg1;
return temp;
}
public int size() {
return count;
}
public List subList(int arg0, int arg1) {
// TODO Auto-generated method stub
return null;
}
public Object[] toArray() {
Object[] obj = new Object[count];
for (int i = 0; i < count; i++) {
obj[i] = data[i];
}
return obj;
}
public Object[] toArray(Object[] arg0) {
// TODO Auto-generated method stub
return null;
}
}
////////////////////////////////////////
package Day12;
public class MyArrayListTest {
public static void main(String[] args) {
MyArrayList mal = new MyArrayList(3);
try {
for (int i = 0; i < 5; i++) {
mal.add(i * 3);
}
mal.add(3, 100);
} catch (Exception e) {
System.out.println("I catch1:"+e);
}
try
{
for(int i=0;i<mal.size();i++){
System.out.println(mal.get(i));
}
}catch(Exception e){
System.out.println("I catch2:"+e);
}
System.out.println(mal.contains(100));
System.out.println(mal.size());
for(Object o:mal.toArray()){
System.out.print(o+"/t");
}
System.out.println("//////////////////////////");
mal.set(3, 200);
for(Object o:mal.toArray()){
System.out.print(o+"/t");
}
System.out.println("//////////////////////////");
mal.remove(3);
for(Object o:mal.toArray()){
System.out.print(o+"/t");
}
System.out.println("//////////////////////////");
Integer i=9;
mal.remove(3);
for(Object o:mal.toArray()){
System.out.print(o+"/t");
}
System.out.println("//////////////////////////");
System.out.println(mal.isEmpty());
System.out.println("//////////////////////////");
System.out.println(mal.get(3));
System.out.println("===========================");
for(Object obj:mal){
System.out.println("/t"+obj);
}
}
}