当前位置:网站首页>Implement interface Iterable & lt; T>
Implement interface Iterable & lt; T>
2022-07-02 07:23:00 【Drizzle】
Implementation interface Interface Iterable
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
So what is enhanced for statement Well ? see Java Language Specification, Do not go into , Similar to the following for sentence .
for (T t : this)
action.accept(t);
Then we need to implement the interface , What method functions do you need to implement ? you 're right , Namely Iterator<T> iterator()
This method . This method ,Returns an iterator over elements of type T.
Realize this method , I need to return a Iterator<T>
The object of ,Iterator<T>
It's also an interface , We create a corresponding object , You need to implement this interface class first . The methods that this interface class needs to implement are :
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.
According to these known , We can start to implement this interface .
Complete code
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);
}
}
}
边栏推荐
- 【信息检索导论】第六章 词项权重及向量空间模型
- 软件开发模式之敏捷开发(scrum)
- [introduction to information retrieval] Chapter 6 term weight and vector space model
- Pyspark build temporary report error
- 【Ranking】Pre-trained Language Model based Ranking in Baidu Search
- ORACLE EBS中消息队列fnd_msg_pub、fnd_message在PL/SQL中的应用
- Oracle EBS ADI development steps
- Oracle EBS database monitoring -zabbix+zabbix-agent2+orabbix
- Alpha Beta Pruning in Adversarial Search
- sparksql数据倾斜那些事儿
猜你喜欢
腾讯机试题
[model distillation] tinybert: distilling Bert for natural language understanding
The first quickapp demo
Cognitive science popularization of middle-aged people
Oracle 11g uses ords+pljson to implement JSON_ Table effect
Oracle EBS ADI development steps
叮咚,Redis OM对象映射框架来了
【信息检索导论】第七章搜索系统中的评分计算
【信息检索导论】第一章 布尔检索
中年人的认知科普
随机推荐
Three principles of architecture design
ERNIE1.0 与 ERNIE2.0 论文解读
ORACLE APEX 21.2安装及一键部署
Oracle apex 21.2 installation and one click deployment
【论文介绍】R-Drop: Regularized Dropout for Neural Networks
ssm+mysql实现进销存系统
Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory'
Check log4j problems using stain analysis
Delete the contents under the specified folder in PHP
MapReduce concepts and cases (Shang Silicon Valley Learning Notes)
Oracle segment advisor, how to deal with row link row migration, reduce high water level
华为机试题-20190417
Only the background of famous universities and factories can programmers have a way out? Netizen: two, big factory background is OK
Oracle rman自动恢复脚本(生产数据向测试迁移)
2021-07-05c /cad secondary development create arc (4)
MapReduce与YARN原理解析
【信息检索导论】第三章 容错式检索
【信息检索导论】第七章搜索系统中的评分计算
Oracle EBs and apex integrated login and principle analysis
Jordan decomposition example of matrix