当前位置:网站首页>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);
}
}
}
边栏推荐
- 叮咚,Redis OM对象映射框架来了
- 【Torch】最简洁logging使用指南
- 使用 Compose 实现可见 ScrollBar
- Pratique et réflexion sur l'entrepôt de données hors ligne et le développement Bi
- Alpha Beta Pruning in Adversarial Search
- ORACLE 11.2.0.3 不停机处理SYSAUX表空间一直增长问题
- DNS攻击详解
- 【BERT,GPT+KG调研】Pretrain model融合knowledge的论文集锦
- Oracle EBS数据库监控-Zabbix+zabbix-agent2+orabbix
- Sqli-labs customs clearance (less1)
猜你喜欢

Analysis of MapReduce and yarn principles

如何高效开发一款微信小程序

MySQL has no collation factor of order by
![[paper introduction] r-drop: regulated dropout for neural networks](/img/09/4755e094b789b560c6b10323ebd5c1.png)
[paper introduction] r-drop: regulated dropout for neural networks

SSM laboratory equipment management

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

DNS attack details

sparksql数据倾斜那些事儿

ORACLE EBS 和 APEX 集成登录及原理分析

【信息检索导论】第二章 词项词典与倒排记录表
随机推荐
外币记账及重估总账余额表变化(下)
The boss said: whoever wants to use double to define the amount of goods, just pack up and go
Typeerror in allenlp: object of type tensor is not JSON serializable error
SSM second hand trading website
RMAN增量恢复示例(1)-不带未备份的归档日志
Get the uppercase initials of Chinese Pinyin in PHP
parser. parse_ Args boolean type resolves false to true
Conda 创建,复制,分享虚拟环境
How to efficiently develop a wechat applet
【Ranking】Pre-trained Language Model based Ranking in Baidu Search
SSM garbage classification management system
@Transitional step pit
架构设计三原则
一份Slide两张表格带你快速了解目标检测
view的绘制机制(一)
A summary of a middle-aged programmer's study of modern Chinese history
ORACLE APEX 21.2安裝及一鍵部署
2021-07-19c CAD secondary development creates multiple line segments
Sqli labs customs clearance summary-page1
MySQL无order by的排序规则因素