Java自定义实现迭代器

简化迭代器原理

package cn.bjsxt.interator;
/**
 * 简化迭代器原理
 * hasNext
 * next
 * @author Administrator
 *
 */
public class MyArrayList {
	private String[] elem = {"a","b","c","d","e","f","g"};
	private int size = elem.length;
	private int curror = -1;
	
	/**
	 * 判断是否存在下一个元素
	 * @return
	 */
	public boolean hasNext() {
		return curror+1 < size; //指向下一个元素 
	}
	
	/**
	 * 获取下一个元素
	 */
	public String next() {
		curror++;         //移动一次
		return elem[curror];
	}
	
	
	public static void main(String[] args) {
		MyArrayList list = new MyArrayList();
		while(list.hasNext()) {
			System.out.println(list.next());
		}
	}
	
}

简化迭代器原理 加入接口 提供方法

package cn.bjsxt.interator;

import java.util.Iterator;

/**
 * 简化迭代器原理 加入接口 提供方法
 * hasNext
 * next
 * @author Administrator
 *
 */

public class MyArrayList implements java.lang.Iterable<String> {
	private String[] elem = {"a","b","c","d","e","f","g"};
	private int size = elem.length;
	
	
	/**
	 * 匿名内部类
	 * @return
	 */	
	public Iterator<String> iterator(){
		return new Iterator<String>(){
			
			private int curror = -1;
			
			public boolean hasNext() {
				return curror+1 < size;
			}
			
			public String next() {
				curror++;
				return elem[curror];
			}
		};          //不要忘记分号
	}
	
	
	
	public static void main(String[] args) {
		MyArrayList list = new MyArrayList();
		Iterator<String> it = list.iterator();
		while(it.hasNext()) {
			System.out.println(it.next());
		}
		
		System.out.println("增强for,必须实现java.lang.Iterable接口,重写iterator方法");
		for(String temp:list) {  //增加了java.lang.Iterable<String记得查看
			System.out.println(temp);   //不考虑下标时使用增强for
		} 
	}
	
}
//实例

package cn.bjsxt.col;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 *
添加元素 C
boolean add(E e)   
查看元素 R
size() 返回容器大小
contains(Object o) 判断是否存在元素 建议重写equals比较内容
isEmpty()  判断容器是否为空
删除元素 D
remove(Object o) 删除指定的元素,建议自定义类型重写equals
clear()  清空容器
其他方法:toArray()  toArray(T[] a) 

遍历:查看整个容器
foreach
iterator() 迭代器
没有通过下标访问的for





 * @author Administrator
 *
 */
public class CollectionDemo01 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//多态 :共性 看不到新增方法
		Collection<String> col =new ArrayList<String>();
		create(col);
		retrieve(col);
		delete(col);
		
		System.out.println(col.size());
		
		
		create(col);
		String[] arr =col.toArray(new String[0]);
		System.out.println(arr.length);
		System.out.println(arr[0]);
		iterCol(col);
		
	}
	/**
	 * 遍历容器
	 */
	public static void iterCol(Collection<String> col){
		System.out.println("=========增强for foreach 不考虑下标======");
		for(String temp:col){
			System.out.println(temp);
		}
		System.out.println("=========迭代器======");
		Iterator<String>  it =col.iterator();
		while(it.hasNext()){
			String temp =it.next();
			System.out.println(temp);
		}
	}
	
	
	/**
	 *删除
	 * 1、remove(对象)
	 * 2、clear() 清空容器
	 * @param col
	 */
	public static void delete(Collection<String> col){
		boolean flag =col.remove("日本");
		System.out.println(flag);
		col.clear();
	}
	/**
	 * 查看
	 * 1、大小
	 * 2、查看
	 * 3、是否为空
	 * @param col
	 */
	public static void retrieve(Collection<String> col){
		System.out.println("容器的大小:"+col.size());
		System.out.println("查看"+col.contains("美国"));
		System.out.println("容器是否存在元素"+col.isEmpty());
	}
	/**
	 * 添加元素
	 * 1、在容器最后添加
	 * @param col
	 */
	public static void create(Collection<String> col){
		col.add("美国");
		col.add("中国");
		
	}

}

接口实例

package cn.bjsxt.col;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * 添加若干个员工,求出员工的总工资,平均工资
 * @author Administrator
 *
 */
public class CollectionDemo02 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Collection<Employee> co=new ArrayList<Employee>();
		Employee ee = new Employee("老马",199999);
		co.add(ee);
		co.add(ee);
		co.add(new Employee("老高",99999));
		co.add(new Employee("老裴",1099999));
		
		//遍历
		Iterator<Employee> it = co.iterator();
		double total =0.0;
		while(it.hasNext()){
			Employee temp = it.next();
			total +=temp.getSalary();
		}
		int size =co.size();
		System.out.println(size);
		System.out.println("总工资为:"+total+",平均薪水"+total/size);
		
	}

}

 

  • 8
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值