当前位置:网站首页>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);
}
}
}
边栏推荐
- SSM personnel management system
- MapReduce concepts and cases (Shang Silicon Valley Learning Notes)
- Implementation of purchase, sales and inventory system with ssm+mysql
- 第一个快应用(quickapp)demo
- ERNIE1.0 与 ERNIE2.0 论文解读
- Build FRP for intranet penetration
- 【Torch】解决tensor参数有梯度,weight不更新的若干思路
- PHP uses the method of collecting to insert a value into the specified position in the array
- ARP attack
- 【信息检索导论】第二章 词项词典与倒排记录表
猜你喜欢
![[model distillation] tinybert: distilling Bert for natural language understanding](/img/c1/e1c1a3cf039c4df1b59ef4b4afbcb2.png)
[model distillation] tinybert: distilling Bert for natural language understanding

Message queue fnd in Oracle EBS_ msg_ pub、fnd_ Application of message in pl/sql

读《敏捷整洁之道:回归本源》后感

外币记账及重估总账余额表变化(下)

SSM学生成绩信息管理系统

ssm+mysql实现进销存系统

SSM personnel management system

ORACLE 11G利用 ORDS+pljson来实现json_table 效果
![[introduction to information retrieval] Chapter 6 term weight and vector space model](/img/42/bc54da40a878198118648291e2e762.png)
[introduction to information retrieval] Chapter 6 term weight and vector space model

Practice and thinking of offline data warehouse and Bi development
随机推荐
Oracle段顾问、怎么处理行链接行迁移、降低高水位
【MEDICAL】Attend to Medical Ontologies: Content Selection for Clinical Abstractive Summarization
【MEDICAL】Attend to Medical Ontologies: Content Selection for Clinical Abstractive Summarization
类加载器及双亲委派机制
软件开发模式之敏捷开发(scrum)
Yaml file of ingress controller 0.47.0
Oracle apex 21.2 installation and one click deployment
ORACLE 11.2.0.3 不停机处理SYSAUX表空间一直增长问题
Oracle segment advisor, how to deal with row link row migration, reduce high water level
Delete the contents under the specified folder in PHP
【信息检索导论】第一章 布尔检索
中年人的认知科普
Cognitive science popularization of middle-aged people
@Transational踩坑
一个中年程序员学习中国近代史的小结
Oracle RMAN automatic recovery script (migration of production data to test)
view的绘制机制(二)
parser. parse_ Args boolean type resolves false to true
RMAN incremental recovery example (1) - without unbacked archive logs
【MEDICAL】Attend to Medical Ontologies: Content Selection for Clinical Abstractive Summarization