当前位置:网站首页>实现接口 Interface Iterable<T>
实现接口 Interface Iterable<T>
2022-07-02 06:25:00 【霏霏小雨】
Java Doc
public interface Iterable
Implementing this interface allows an object to be the target of the enhanced for statement (sometimes called the “for-each loop” statement).
Since: 1.5
那么什么是 enhanced for statement 呢?查看 Java Language Specification,不做赘述,类似于下面的这种 for 语句。
for (T t : this)
action.accept(t);
那么我们要实现接口,具体需要实现什么方法函数呢?没错,就是Iterator<T> iterator()这个方法。这一方法,Returns an iterator over elements of type T.
实现这一方法,需要返回一个Iterator<T>的对象,Iterator<T>同样是一个接口,我们创建一个相应的对象,就需要先实现这个接口类。这个接口类需要实现的方法有:
boolean hasNext();
Returns true if the iteration has more elements. (In other words, returns true if next() would return an element rather than throwing an exception.)
T next();
Returns the next element in the iteration.
根据这些已知,我们就可以开始实现这个接口了。
完整代码
import java.util.Arrays;
import java.util.Iterator;
public class ImplementIterable implements Iterable<String> {
private int size;
private int current;
private Object [] o;
ImplementIterable() {
size = 0;
o = new Object [1];
}
public void add(String s) {
if(size == o.length) {
o= Arrays.copyOf(o, o.length<<1);
}
o[size ++] = s;
}
@Override
public Iterator<String> iterator() {
class I implements Iterator<String> {
I() {
current = 0;
}
@Override
public boolean hasNext() {
if(current < size) {
return true;
}
return false;
}
@Override
public String next() {
return o[current ++].toString();
}
}
return new I();
}
public static void main(String[] args) {
ImplementIterable i = new ImplementIterable();
i.add("a");
i.add("b");
i.add("c");
for(String s : i) {
System.out.println(s);
}
}
}
边栏推荐
- php中的二维数组去重
- ssm人事管理系统
- 2021-07-05C#/CAD二次开发创建圆弧(4)
- Sqli-labs customs clearance (less2-less5)
- Yolov5 practice: teach object detection by hand
- Oracle 11g uses ords+pljson to implement JSON_ Table effect
- Brief analysis of PHP session principle
- ORACLE EBS DATAGUARD 搭建
- In depth study of JVM bottom layer (IV): class file structure
- 架构设计三原则
猜你喜欢

Oracle EBs and apex integrated login and principle analysis

SQLI-LABS通关(less15-less17)

UEditor . Net version arbitrary file upload vulnerability recurrence

离线数仓和bi开发的实践和思考

oracle apex ajax process + dy 校验

Oracle EBS数据库监控-Zabbix+zabbix-agent2+orabbix

Sqli labs customs clearance summary-page2

SQLI-LABS通关(less18-less20)

CSRF attack

Explain in detail the process of realizing Chinese text classification by CNN
随机推荐
Use of interrupt()
Spark的原理解析
Take you to master the formatter of visual studio code
2021-07-05C#/CAD二次开发创建圆弧(4)
The boss said: whoever wants to use double to define the amount of goods, just pack up and go
CSRF攻击
Sqli Labs clearance summary - page 2
sqli-labs通关汇总-page1
Oracle RMAN semi automatic recovery script restore phase
Oracle EBs and apex integrated login and principle analysis
Yolov5 practice: teach object detection by hand
ORACLE 11G SYSAUX表空间满处理及move和shrink区别
CRP实施方法论
Oracle rman自动恢复脚本(生产数据向测试迁移)
Pyspark build temporary report error
TCP攻击
SQLI-LABS通关(less6-less14)
JS judge whether the object is empty
In depth study of JVM bottom layer (IV): class file structure
JS to determine whether there is a value in the object in the array