List

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class MyArrayList {
	private Object[] arr;
	private int pointer;
	
	public MyArrayList(){
		arr = new Object[10];
		pointer = -1;
	} 
	
	public MyArrayList(int i){
		arr = new Object[i];
		pointer = -1;
	}
	
	public boolean add(Object e) {
		// 在添加之前,判断数组是否够用?如果不够用?扩容!
		if (pointer >= (arr.length - 1)) {
			// 扩容
			Object[] temp = new Object[arr.length + 10];
			System.arraycopy(arr, 0, temp, 0, arr.length);
			this.arr = temp;
		}
		this.arr[++pointer] = e;
		return true;
	}
	
	public boolean addAll(Collection c) {
		Iterator it = c.iterator();
		while(it.hasNext()){
			this.add(it.next());
		}
		return true;
	}


	public boolean addAll(int index, Collection c) {
		// TODO Auto-generated method stub
		return false;
	}


	public void clear() {
		this.arr = new Object[10];
		this.pointer = -1;
	}


	public boolean contains(Object o) {
		boolean flag = false;
		for (int i = 0; i < this.arr.length; i++) {
			if (o == arr[i] || o.equals(arr[i])) {
				flag = true;
				break;
			}
		}
		return flag;
	}


	public boolean containsAll(Collection c) {
		boolean flag = true;
		Iterator it = c.iterator();
		while(it.hasNext()){
			if(!this.contains(it.next())){
				flag = false;
				break;
			}
		}
		return flag;
	}


	public Object get(int index) {
		if (index > this.arr.length - 1) {
			throw new IndexOutOfBoundsException("给定下标越界");
		} else {
			return this.arr[index];
		}
	}


	public int indexOf(Object o) {
		int index = -1;
		for(int i=0;i<arr.length;i++ ){
			index++;
			if(contains(o))break;
		}
		if(index>=arr.length)index=-1;
		return index;
	}


	public boolean isEmpty() {
		
		return false;
	}


	public Iterator iterator() {
		return null;
	}


	public int lastIndexOf(Object o) {
		// TODO Auto-generated method stub
		return 0;
	}


	public ListIterator listIterator() {
		// TODO Auto-generated method stub
		return null;
	}


	public ListIterator listIterator(int index) {
		// TODO Auto-generated method stub
		return null;
	}


	public boolean remove(Object o) {
		if(!contains(o)){
			return false;
		}else{
			int index = indexOf(o);
			remove(index);
			return true;
		}
	}


	public Object remove(int index) {
		Object obj = null;
		if(index<0||index>arr.length){
			throw new IndexOutOfBoundsException();
		}else{
			obj = arr[index];
			if(index==arr.length-1){
				arr[index]=null;
			}else{
				for(int i=index;i<arr.length-1;i++){
					arr[i]=arr[i+1];
				}
				arr[arr.length-1]=null;
			}
		}
		return obj;
	}


	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) {
		// TODO Auto-generated method stub
		return null;
	}


	public int size() {
		return this.pointer + 1;
	}


	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;
	}
	
}

你可能感兴趣的:(C++,c,C#)