import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class MyArrayList implements List{
private Object[] data;
private int count;
//(4) int i=0;//外部类的属性
public MyArrayList(){
this(10);
}
public MyArrayList(int initCap){
data=new Object[initCap];
count=0;
}
public void add(int index, Object element) {
//1,判断index是否合法
if(index<0 || index >count){
throw new IndexOutOfBoundsException("无效的下标:"+index);
}
//2,判断数组空间是否已满,若满了,则扩容!
if(count==data.length){
//扩容:
//(1) 定义一个新数组,是原数组长度的两倍
Object[] newData=new Object[data.length*2];
//(2)把原数组中的元素拷贝到新数组中来
System.arraycopy(data, 0, newData, 0, data.length);
//(3)将新数组赋值给data
data=newData;
}
//移动数组元素,从index到count-1位置的所有元素依次向后移动一个位置。
for(int i=count-1;i>=index;i--){
data[i+1]=data[i];
}
data[index]=element;
count++;
}
public boolean add(Object e) {
add(count,e);
return true;
}
public boolean addAll(Collection c) {
// TODO Auto-generated method stub
return false;
}
public boolean addAll(int index, Collection c) {
// TODO Auto-generated method stub
return false;
}
public void clear() {
for(int i=0;i<count;i++){
data[i]=null;
}
count=0;
System.gc();
}
public boolean contains(Object o) {
for(int i=0;i<count;i++){
if(data[i].equals(o)){
return true;
}
}
return false;
}
public boolean containsAll(Collection c) {
// TODO Auto-generated method stub
return false;
}
public Object get(int index) {
if(index<0 || index>=count){
throw new IndexOutOfBoundsException ("无效的下标:"+index);
}
return data[index];
}
public int indexOf(Object o) {
for(int i=0;i<count;i++){
if(data[i].equals(o)){
return i;
}
}
return -1;
}
public boolean isEmpty() {
return count==0;
}
public Iterator iterator() {
//(3) int i=0;//iterator方法中的局部变量
return new Iterator(){
//(2) int i=0;//内部类的属性
private int i=0;
public boolean hasNext() {
//(1) int i=0;//hasNext方法中的局部变量
//??????
return i<count;
}
public Object next() {
//??????
return data[i++];
//A i
//B i+1
//C i++
//D ++i
}
public void remove() {
MyArrayList.this.remove(--i);
}
};
}
public int lastIndexOf(Object o) {
for(int i=count-1;i>=0;i--){
if(data[i].equals(o)){
return i;
}
}
return -1;
}
public ListIterator listIterator() {
// TODO Auto-generated method stub
return null;
}
public ListIterator listIterator(int index) {
// TODO Auto-generated method stub
return null;
}
public Object remove(int index) {
if(index<0 || index >=count){
throw new IndexOutOfBoundsException("无效的下标:"+index);
}
Object temp=data[index];
for(int i=index+1;i<=count-1;i++){
data[i-1]=data[i];
}
count--;
return temp;
}
public boolean remove(Object o) {
int index=indexOf(o);
if(index<0){
return false;
}else{
remove(index);
return true;
}
}
public boolean removeAll(Collection c) {
// TODO Auto-generated method stub
return false;
}
public boolean retainAll(Collection c) {
// TODO Auto-generated method stub
return false;
}
public Object set(int index, Object element) {
if(index<0 || index >=count){
throw new IndexOutOfBoundsException("无效的下标:"+index);
}
Object temp=data[index];
data[index]=element;
return temp;
}
public int size() {
return count;
}
public List subList(int fromIndex, int toIndex) {
// TODO Auto-generated method stub
return null;
}
public Object[] toArray() {
// TODO Auto-generated method stub
return null;
}
public Object[] toArray(Object[] a) {
// TODO Auto-generated method stub
return null;
}
}