数据结构之ArrayList

package MYArrayList;
/**
 * 
 * @author zz384dian
 * @time 2018-2-15
 * @param 
 * @cotent ArrayList的实现
 */
public class MyArrayList {

	private Integer size;
	private Object objs[];
	
	public MyArrayList(){
		this(10);
	}
	
	public MyArrayList(Integer size){
		objs = new Object[size];
		this.size = 0;
	}
	
	//扩容
	public void ensureCapacity(){
		Object temp[] = objs;
		objs = new Object[objs.length + 10];
		for(int i = 0;i=index;i--){
			objs[i+1] = objs[i];
		}
		objs[index] = obj;
		size++;
	}
	//删除
	public void remove(int index){
		for(int i = index;i iterator(){
		return new MyIterator(size,objs);
	}
}
package MYArrayList;
/**
 * 
 * @author zz384dian
 * @time 2018-2-15
 * @content Iterator的实现
 * @param 
 */
public class MyIterator {

	private Integer size;
	private Object objs[];
	private Integer current;
	
	public MyIterator(int size, Object objs[]){
		this.size = size;
		this.objs = objs;
		current = -1;
	}
	public E hasNext(){
		return (E)objs[current];
	}
	
	public boolean Next(){
		current++;
		if(current < size){
			return true;
		}
		return false;
	}
}

package MYArrayList;
/**
 * 
 * @author zz384dian
 * @time 2018-2-15
 * @content Student类
 */
public class Student {
	
	private String name;
	private Integer age;
	
	public Student() {
		super();
	}

	public Student(String name, Integer age) {
		super();
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}

}

package MYArrayList;
/**
 * 
 * @author zz384dian
 * @time 2018-2-15
 * @content 一个测试MyArrayList的Test类
 */
public class Test {

	public static void main(String[] args) {
		// TODO Auto-genrated method stub
		MyArrayList al = new MyArrayList<>();
		al.add(new Student("zs",26));
		al.add(new Student("jyh",30));

		al.insert(0, new Student("k",26));
		//System.out.println(al.size());
		
		MyIterator iter = al.iterator();
		
		while(iter.Next()){
			System.out.println(iter.hasNext());
		}
		
		
	}

}


你可能感兴趣的:(数据结构与算法分析)