package MYArrayList;
/**
*
* @author zz384dian
* @time 2018-2-15
* @param <E>
* @cotent ArrayList的实现
*/
public class MyArrayList<E> {
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<temp.length;i++){
objs[i] = temp[i];
}
}
//添加
public void add(Object obj){
if(size == objs.length){
ensureCapacity();
}
objs[size++] = obj;
}
//插入
public void insert(int index,Object obj){
if(size == objs.length){
ensureCapacity();
}
for(int i = size-1;i>=index;i--){
objs[i+1] = objs[i];
}
objs[index] = obj;
size++;
}
//删除
public void remove(int index){
for(int i = index;i<size;i++){
objs[i-1] = objs[i];
}
size--;
}
//置空
public void makeEmpty(){
size = 0;
}
//大小
public int size(){
return size;
}
//判断是否为空
public boolean empty(){
if(size == 0){
return true;
}
return false;
}
//迭代器
public MyIterator<E> iterator(){
return new MyIterator(size,objs);
}
}
package MYArrayList;
/**
*
* @author zz384dian
* @time 2018-2-15
* @content Iterator的实现
* @param <E>
*/
public class MyIterator<E> {
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<Student> 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());
}
}
}