1.这是个比较古老的例子,
用了4个函数来实现Iterator,现在Java 的Iterator 应该只要实现 hasNext(), next(), remove() 这三个函数就好了。
2. 这个例子比较好,从这里抄了一个例子,使用了一个private inner class 来实现的Iterator,我在某一本书上看说用private inner class 比public inner class 要好。
http://www.journaldev.com/1716/iterator-design-pattern-java
-
package iterator;
-
-
import java.util.ArrayList;
-
import java.util.List;
-
-
enum ChannelTypeEnum{
-
ENGLISH,HINDI,FRENCH,ALL;
-
}
-
-
class Channel{
-
private double frequency;
-
private ChannelTypeEnum TYPE;
-
-
public Channel(double freq, ChannelTypeEnum TYPE){
-
this.TYPE= TYPE;
-
this.frequency = freq;
-
}
-
-
public double getFrequency(){
-
return frequency;
-
}
-
-
public ChannelTypeEnum getType(){
-
return TYPE;
-
}
-
-
@Override
-
public String toString(){
-
return "Frequery ="+this.frequency+", Type="+this.TYPE;
-
}
-
}
-
-
interface ChannelIterator{
-
public boolean hasNext();
-
public Channel next();
-
}
-
-
interface ChannelCollection{
-
public void addChannel(Channel c);
-
public void removeChannel(Channel c);
-
public ChannelIterator iterator(ChannelTypeEnum type);
-
}
-
-
class ChannelCollectionImpl implements ChannelCollection{
-
private List<Channel> channelList;
-
-
//Constructor
-
public ChannelCollectionImpl(){
-
channelList = new ArrayList<Channel>();
-
}
-
-
public void addChannel(Channel c){
-
this.channelList.add(c);
-
}
-
-
public void removeChannel(Channel c){
-
this.channelList.remove(c);
-
}
-
-
@Override
-
public ChannelIterator iterator(ChannelTypeEnum type){
-
return new ChannelIteratorImpl(type, this.channelList);
-
}
-
-
private class ChannelIteratorImpl implements ChannelIterator{
-
private ChannelTypeEnum type;
-
private List<Channel> channels;
-
private int position;
-
-
public ChannelIteratorImpl(ChannelTypeEnum ty, List<Channel> channelList){
-
this.type = ty;
-
this.channels = channelList;
-
}
-
-
@Override
-
public boolean hasNext(){
-
while(position < channels.size()){
-
Channel c = channels.get(position);
-
if(c.getType().equals(type) || type.equals(ChannelTypeEnum.ALL)){
-
return true;
-
}else
-
position ++;
-
}
-
return false;
-
}
-
-
@Override
-
public Channel next(){
-
Channel c = channels.get(position);
-
position ++;
-
return c;
-
}
-
}
-
}
-
-
-
public class IteratorDemo {
-
public static void main(String[] args){
-
ChannelCollection channels = populateChannels();
-
ChannelIterator baseIterator = channels.iterator(ChannelTypeEnum.ALL);
-
while(baseIterator.hasNext()){
-
Channel c = baseIterator.next();
-
System.out.println(c.toString());
-
}
-
System.out.println("********");
-
//Channel Type Iterator
-
ChannelIterator englishIterator = channels.iterator(ChannelTypeEnum.ENGLISH);
-
while(englishIterator.hasNext()){
-
Channel c = englishIterator.next();
-
System.out.println(c.toString());
-
}
-
}
-
-
private static ChannelCollection populateChannels(){
-
ChannelCollection channels = new ChannelCollectionImpl();
-
channels.addChannel(new Channel(98.5, ChannelTypeEnum.ENGLISH));
-
channels.addChannel(new Channel(99.5, ChannelTypeEnum.HINDI));
-
channels.addChannel(new Channel(100.5, ChannelTypeEnum.FRENCH));
-
channels.addChannel(new Channel(101.5, ChannelTypeEnum.ENGLISH));
-
channels.addChannel(new Channel(102.5, ChannelTypeEnum.HINDI));
-
channels.addChannel(new Channel(103.5, ChannelTypeEnum.FRENCH));
-
channels.addChannel(new Channel(104.5, ChannelTypeEnum.ENGLISH));
-
channels.addChannel(new Channel(105.5, ChannelTypeEnum.HINDI));
-
channels.addChannel(new Channel(106.5, ChannelTypeEnum.FRENCH));
-
return channels;
-
}
-
}
3. 记录下最近碰到的一个shell脚本出错的问题。
$1 在一个脚本调用其他脚本的过程中,如果有set 语句,会改变调用脚本的位置参数。
这是我要运行的脚本
-
#! /bin/bash -x
-
-
echo "\$1 is $1"
-
if [ -x "/etc/profile.d/sinit.sh" ];then
-
source /etc/profile.d/sinit.sh
-
fi
-
echo "\$1 is $1"
-
case "$1" in
-
start) echo "$1 is start";;
-
stop) echo "$1 is stop";;
-
*) echo "wrong coice";;
-
esac
这是另外一个被调用的脚本,当然其实这个应该算是个文本文件,而不能算是脚本,因为并没有#!(shebang), 当然在我实验的过程中,即使在第一行加上#!(shebang) ,
调用脚本的$1 仍然会被污染。不用.用source 也是一样。
-
/etc/profile.d/sinit.sh
-
TMOUT=1800
-
export TMOUT
-
set autologout=30
请看执行, source 后$1就被污染了。
-
./echo_env2.sh start
-
+ echo '$1 is start'
-
$1 is start
-
+ '[' -x /etc/profile.d/sinit.sh ']'
-
+ source /etc/profile.d/sinit.sh
-
++ TMOUT=1800
-
++ export TMOUT
-
++ set autologout=30
-
+ echo '$1 is autologout=30'
-
$1 is autologout=30
-
+ case "$1" in
-
+ echo 'wrong coice'
-
wrong coice
阅读(1162) | 评论(0) | 转发(0) |