可迭代的集合类型使用foreach语句
在学习算法这本书图论那一部分的时候,接触到了几个类似for(int w:G.adj(v)),的语句,不是很理解,就去百度,发现这是一种叫做foreach的语法,在书的76页有讲到,但是之前没认真看书,下面是有关我的foreach学习笔记:
foreach用于访问一个集合内的所有元素,适用于遍历数组和集合类。注意foreach不是关键字,书上对foreach的解释的大概意思是:可以将该fo语句看做是对于集合中的每个元素,执行以下的代码。它不需要知道集合的具体细节,只是处理集合中的每一个元素。
foreach的大概格式是;
for(元素类型t 元素变量x : 遍历对象obj){
引用了x的java语句;
}
通过举例子,或许会更加了解foreach:
在一维数组中使用:
int array[ ] = {1,2,3,4};
for(int x : array) //比平常的写法要简单
{
System.out.println("x");
}
在二维数组中的使用:
int array[ ][ ] = { {4,3},{1,2}} //类似也可以写3维数组
for(int x[ ] ;array){
for(int b ; x){
System.out.println("e");
}
}
不过foreach语句应该更多在遍历集合类型,且集合类型是要求可迭代的,即要求实现Iterable<T>接口,算法书上的Stack ,Queue以及Bag都有实现iterable接口,并返回一个Iterator对象。
其中Iterator类必须包括两个方法:
1.hasNext(),返回一个布尔值。
2.next(),返回集合中的一个元素。
这里贴一个Bag的代码:
import java.util.Iterator;
public class Bag<Item> implements Iterable<Item>{
private Node first;
private int N;
private class Node
{
Item item;
Node next;
}
public void add(Item item)
{
//和Stack的push方法一样
Node oldfirst = first;
first = new Node();
first.item = item;
first.next = oldfirst;
N++;
}
//这里实现了接口并且重写了俩方法
private class ListIterator implements Iterator<Item>
{
private Node current = first;
public boolean hasNext() //检测currents是否为空
{return current != null;}
public void remove(){};
public Item next()
{
Item item = current.item;
current = current.next; //实例变量来记录当前节点
return item ;
}
}
//返回了一个iterabor对象
public Iterator<Item> iterator()
{ //
return new ListIterator();
}
}
实现了iterator接口并重写了那俩方法后,之后就可以用foreach语句了,例子如下:
Bag<Transaction> collection = new Bag<Transaction>;//在Bag中维护一个可交易集合,和书上的类似
//此处省略具体实现代码......................;
for(Transaction t : collection)
{ System.out.println(t);}
可迭代的集合类型使用foreach语句的更多相关文章
- C# 通过IEnumberable接口和IEnumerator接口实现自定义集合类型foreach功能
1.IEnumerator和IEnumerable的作用 其实IEnumerator和IEnumerable的作用很简单,就是让除数组和集合之外的类型也能支持foreach循环,至于foreach循环 ...
- C# 通过IEnumberable接口和IEnumerator接口实现泛型和非泛型自定义集合类型foreach功能
IEnumerator和IEnumerable的作用 其实IEnumerator和IEnumerable的作用很简单,就是让除数组和集合之外的类型也能支持foreach循环,至于foreach循环,如 ...
- C# foreach语句
一.C# foreach语句 foreach语句能够对实现Ienumerable接口的容器进行遍历,并提供一个枚举器来实现Ienumerable接口.foreach语句为数组或对象集合中的各个元素执行 ...
- MSIL实用指南-生成foreach语句
foreach可以迭代数组或者一个集合对象.foreach语句格式是它的生成步骤是foreach (<成员> in <集合>) <循环体> 一.声明三个变量,loc ...
- 自定义一个可以使用foreach语句进行迭代的类(IEnumerable)
在c#中,凡是实现了IEnumerable接口的数据类型都可以用foreach语句进行迭代访问.所以,我们要定义一个可以使用foreach进行迭代访问的类,就必须要实现IEnumerable接口. / ...
- mybatis里的foreach语句
相信用了Mybatis的朋友们,都曾有一个疑惑,就是foreach是怎么用的,下面我就简单讲讲我的理解: foreach主要用在SQL语句中迭代一个集合.foreach元素的属性主要由item,ind ...
- MyBatis的foreach语句详解
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有 item,index,collection,open,separator,close.it ...
- MyBatis的foreach语句详解 list array map
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有 item,index,collection,open,separator,close.it ...
- 从foreach语句枚举元素看数组
在foreach语句中使用枚举,可以迭代数组或集合中的元素,且无须知道集合中的元素的个数.如图显示了调用foreach方法的客户端和集合之间的关系.数组或集合实现带GetEnumerator()方法的 ...
随机推荐
- document.body.innerHTML用jquery如何表示
$("body").html('XXXX'); //这个是赋值 $("body").html(); //这个是获取HTML的内容
- @Inject.@Resource.@Autowired 的区别
@Inject:Struts2的注解, @Resource : J2EE提供,用于注入,( j2ee提供的 ) 默认按名称装配,@Resource(name="beanName") ...
- CSS 中区块的使用_宽高属性
width 像素/百分比 区块的宽度 auto height 像素/百分比 区块的高度 auto min-height 像素像素/百分比 区块最小高度 auto max-height 像素像素/百分比 ...
- Spring MVC 下index.jsp访问
spring-mvc.xml配置 <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> <bean class="org.springframework.we ...
- 安装 Visual Stuidio 2010 失败
百思不得其解,尝试解压安装iso文件,解压都正常,怀疑Daemon Tools 是不是有问题? 最终问题还是定位在文件出问题了.SHA值不一样,囧!
- BZOJ 3295 动态逆序对
调了好久.... 转化成三维偏序,cdq处理. 好像比较快? #include<iostream> #include<cstdio> #include<cstring&g ...
- 打印Dom对象的所有属性和方法
<html> <head> <title>Test</title> <meta http-equiv="Content-Type&quo ...
- 【英语】Bingo口语笔记(3) - 无所谓
what's in it for me? 这对我有什么好处?
- 【解题报告】HDU -1142 A Walk Through the Forest
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1142 题目大意:Jimmy要从办公室走路回家,办公室在森林的一侧,家在另一侧,他每天要采取不一样的路线 ...
- Android 开源项目PhotoView源码分析
https://github.com/chrisbanes/PhotoView/tree/master/library 这个就是项目地址,相信很多人都用过,我依然不去讲怎么使用.只讲他的原理和具体实现 ...