接口
package com.ghgcn.chapter03.data;
public interface MyList<T> {
/**
* 是否为空
* @return
*/
public boolean isEmpty();
/**
* 获取大小
* @return
*/
public int size();
/**
* 添加默认添加到最后
* @param e
* @return
*/
public boolean add(T e);
/**
* 添加到指定位置
* @param e
* @return
*/
public boolean add(int index,T e);
/**
* 获取指定位置的元素
* @param index
* @return
*/
public T get(int index);
/**
* 移除指定位置的元素
* @param index
* @return
*/
public boolean remove(int index);
}
自定义迭代器接口
package com.ghgcn.chapter03.data;
import java.util.Iterator;
/**
* 迭代器
* @author Administrator
*
*/
public interface MyIterator<T> {
/**
* 是否有下一下
* @return
*/
public boolean hasNext();
/**
* 下一个元素
* @return
*/
public T next();
}
实现
package com.ghgcn.chapter03.data;
public class MyArrayList<T> implements MyList<T>, MyIterator<T> {
protected int DEFAULT_SIZE = 4;
protected Object[] elements;
private int size = 0;
/**
* 迭代器使用
*/
private int position = 0;
/**
* 初始化 容器大小
*/
public MyArrayList() {
this.elements = new Object[DEFAULT_SIZE];
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public int size() {
return size;
}
@Override
public boolean add(T e) {
checkSize();
elements[size++] = e;
return true;
}
/**
* 添加到指定位置
*/
@Override
public boolean add(int index, T e) {
/**
* 判断下标是否在容器之内
*/
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("index: " + index + " size: " + size);
}
/**
* 判断 空间大小是否扩容量
*/
checkSize();
/**
* 将要插入位置之后的元素后移一位
*/
for (int k = size - 1; k >= index; k--) {
elements[k + 1] = elements[k];
}
/**
* 赋值
*/
elements[index] = e;
/**
* 量加1数
*/
size++;
return true;
}
/**
* 较验证空间大小 扩容
*/
private void checkSize() {
if (size >= elements.length - 1) {
Object[] tmpData = new Object[elements.length * 2];
for (int i = 0; i < elements.length - 1; i++) {
tmpData[i] = elements[i];
}
elements = tmpData;
}
}
/**
* 获取指定位置的元素
*/
@Override
public T get(int index) {
rangeCheck(index);
return elementData(index);
}
/**
* 移除
*/
@Override
public boolean remove(int index) {
/**
* 判断下标是否在容器之内
*/
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("index: " + index + " size: " + size);
}
/**
* 移除元素,将index之后的元素前移
*/
for (int k = index - 1; k < size - 1; k++) {
elements[index] = elements[k + 1];
}
/**
* 把最后一个元素置空
*/
elements[size - 1] = null;
/**
* 数量-1
*/
size--;
return true;
}
/**
* 下标检查
*
* @param index
*/
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(index + "");
}
/**
* 获取指定位置的元素
*
* @param index
* @return
*/
@SuppressWarnings("unchecked")
T elementData(int index) {
return (T) elements[index];
}
@Override
public boolean hasNext() {
if (position >= size || elements[position] == null) {
position=0;
return false;
} else {
return true;
}
}
@Override
public T next() {
T e = elementData(position);
position++;
return e;
}
@Override
public String toString() {
if (!this.hasNext()) {
return "[]";
}
StringBuilder sb = new StringBuilder();
sb.append("[");
while (this.hasNext()) {
sb.append(next() + " ,");
}
sb.deleteCharAt(sb.length() - 1);
sb.append("]");
return sb.toString();
}
/**
* 是否包含
* @param o
* @return
*/
public int contains(Object o) {
if (o == null) {
for (int i = 0; i < size; i++) {
if(elements[i]==null){
return i;
}
}
} else {
for (int i = 0; i < size; i++) {
if(elements[i].equals(o)){
return i;
}
}
}
return -1;
}
/**
* 移除指定对象
* @param o
* @return
*/
public boolean removeObject(Object o){
if (o == null) {
for (int i = 0; i < size; i++) {
if(elements[i]==null){
remove(i);
return true;
}
}
} else {
for (int i = 0; i < size; i++) {
if(elements[i].equals(o)){
remove(i);
return true;
}
}
}
return false;
}
}
测试
package com.ghgcn.chapter03.data;
import java.util.Arrays;
import com.sun.source.tree.WhileLoopTree;
public class MyArrayListTest1 {
public static void main(String[] args) {
MyArrayList<Integer> myArrayList = new MyArrayList<>();
myArrayList.add(5);
myArrayList.add(4);
myArrayList.add(3);
myArrayList.add(2);
myArrayList.add(6);
System.out.println("size "+myArrayList.size());
System.out.println(Arrays.toString(myArrayList.elements));
System.out.println(" 获取指定下标的元素 \n "+myArrayList.get(0));
System.out.println(" 获取指定下标的元素 \n "+myArrayList.get(4));
myArrayList.add(0, 10);
System.out.println(Arrays.toString(myArrayList.elements));
myArrayList.remove(0);
System.out.println(Arrays.toString(myArrayList.elements));
System.out.println(myArrayList.hasNext());
System.out.println("使用定义的迭代器遍历");
while(myArrayList.hasNext()){
System.out.print(myArrayList.next()+" \t");
}
System.out.println("\n 是否包含: "+ myArrayList.contains(7));
System.out.println("\n 移除: "+ myArrayList.removeObject(5));
System.out.println("使用定义的迭代器遍历: "+myArrayList.hasNext());
while(myArrayList.hasNext()){
System.out.print(myArrayList.next()+" \t");
}
System.out.println("\n to String: "+myArrayList.toString());
}
}
结果
image.png学习中,有问题请大家指正
网友评论