MyIterator.java 自定義迭代器
/**
* 自定義迭代器
*
*/
public interface MyIterator {
boolean hasNext();
Object next();
}
MyArraylist.java 自己實(shí)現(xiàn)的Arraylist
import java.util.Arrays;
public class MyArraylist {
private Object[] objs = new Object[10]; //定義數(shù)組渊抄,初始化長度是10
private int index = 0; //數(shù)組下標(biāo)
/**
* 添加
*@param o
*/
public void add(Object o){
if (index == objs.length){
// 擴(kuò)容
extend();
}
objs[index++] =o;
}
/**
* 根據(jù)下標(biāo)獲取對(duì)象
* @param index 下標(biāo)
* @return
*/
public Object get(int index){
return objs[index];
}
/**
* 計(jì)算元素個(gè)數(shù)
* @return
*/
public int size(){
int size = 0;
for (int i = 0; i < objs.length; i++) {
if(objs[i]!=null){
size++;
}
}
return size;
}
/**
* 清空
*/
public void clear(){
for (int i = 0; i < objs.length; i++) {
objs[i] = null;
}
}
/**
* 判斷是否包含
* @param o
* @return
*/
public boolean contains(Object o){
for (int i = 0; i < objs.length; i++) {
if(o!=null&&o.equals(objs[i])){
return true;
}
}
return false;
}
/**
* 根據(jù)下標(biāo)刪除
*/
public void remove(int index){
if (index<0 || index>= this.size()){
System.out.println("下標(biāo)無效炸客,無法刪除征绸。捏膨。捂襟。");
throw new IndexOutOfBoundsException();
}
for (int i = index; i < objs.length; i++) {
objs[i] = objs[i+1];
if (i==this.size()){
break;
}
}
}
/**
* 根據(jù)對(duì)象刪除
* @param o
*/
public void remove(Object o){
int index = getIndexByObject(o);
remove(index);
}
/**
* 根據(jù)對(duì)象獲取對(duì)象的下標(biāo)
* @return
*/
public int getIndexByObject(Object o){
for (int i = 0; i < objs.length; i++) {
if(o!=null&&o.equals(objs[i])){
return i;
}
}
return -1;
}
/**
* 自定義迭代器
*/
public MyIterator iterator(){
return new Iterator();
}
class Iterator implements MyIterator{
private int size = 0;
@Override
public boolean hasNext() {
if(objs[size]!=null) return true;
else return false;
}
@Override
public Object next() {
size++;
return objs[size-1];
}
}
/**
* 擴(kuò)容
*/
private void extend(){
Arrays.copyOf(objs,objs.length+1);
}
}
Test.java 測(cè)試代碼
import java.util.Arrays;
public class MyArraylist {
private Object[] objs = new Object[10]; //定義數(shù)組扔亥,初始化長度是10
private int index = 0; //數(shù)組下標(biāo)
/**
* 添加
*@param o
*/
public void add(Object o){
if (index == objs.length){
// 擴(kuò)容
extend();
}
objs[index++] =o;
}
/**
* 根據(jù)下標(biāo)獲取對(duì)象
* @param index 下標(biāo)
* @return
*/
public Object get(int index){
return objs[index];
}
/**
* 計(jì)算元素個(gè)數(shù)
* @return
*/
public int size(){
int size = 0;
for (int i = 0; i < objs.length; i++) {
if(objs[i]!=null){
size++;
}
}
return size;
}
/**
* 清空
*/
public void clear(){
for (int i = 0; i < objs.length; i++) {
objs[i] = null;
}
}
/**
* 判斷是否包含
* @param o
* @return
*/
public boolean contains(Object o){
for (int i = 0; i < objs.length; i++) {
if(o!=null&&o.equals(objs[i])){
return true;
}
}
return false;
}
/**
* 根據(jù)下標(biāo)刪除
*/
public void remove(int index){
if (index<0 || index>= this.size()){
System.out.println("下標(biāo)無效柄错,無法刪除埃碱。嚎尤。荔仁。");
throw new IndexOutOfBoundsException();
}
for (int i = index; i < objs.length; i++) {
objs[i] = objs[i+1];
if (i==this.size()){
break;
}
}
}
/**
* 根據(jù)對(duì)象刪除
* @param o
*/
public void remove(Object o){
int index = getIndexByObject(o);
remove(index);
}
/**
* 根據(jù)對(duì)象獲取對(duì)象的下標(biāo)
* @return
*/
public int getIndexByObject(Object o){
for (int i = 0; i < objs.length; i++) {
if(o!=null&&o.equals(objs[i])){
return i;
}
}
return -1;
}
/**
* 自定義迭代器
*/
public MyIterator iterator(){
return new Iterator();
}
class Iterator implements MyIterator{
private int size = 0;
@Override
public boolean hasNext() {
if(objs[size]!=null) return true;
else return false;
}
@Override
public Object next() {
size++;
return objs[size-1];
}
}
/**
* 擴(kuò)容
*/
private void extend(){
Arrays.copyOf(objs,objs.length+1);
}
}