当前位置:网站首页>实现接口 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);
}
}
}
边栏推荐
- CRP实施方法论
- Common prototype methods of JS array
- ORACLE APEX 21.2安裝及一鍵部署
- 2021-07-19c CAD secondary development creates multiple line segments
- Flex Jiugongge layout
- Sqli Labs clearance summary - page 2
- MapReduce与YARN原理解析
- Take you to master the formatter of visual studio code
- Changes in foreign currency bookkeeping and revaluation general ledger balance table (Part 2)
- Oracle EBS ADI development steps
猜你喜欢

MySQL无order by的排序规则因素

MySQL中的正则表达式

sqli-labs通關匯總-page2

Basic knowledge of software testing

User login function: simple but difficult

Oracle 11g uses ords+pljson to implement JSON_ Table effect

IDEA2020中PySpark的两表关联(字段名相同)

Check log4j problems using stain analysis

Oracle EBS database monitoring -zabbix+zabbix-agent2+orabbix

Principle analysis of spark
随机推荐
解决万恶的open failed: ENOENT (No such file or directory)/(Operation not permitted)
ORACLE APEX 21.2安装及一键部署
The boss said: whoever wants to use double to define the amount of goods, just pack up and go
CSRF攻击
sqli-labs通关汇总-page4
php中的数字金额转换大写数字
2021-07-17C#/CAD二次开发创建圆(5)
JS to determine whether there is a value in the object in the array
sqli-labs通關匯總-page2
ssm垃圾分类管理系统
在php的开发环境中如何调取WebService?
2021-07-05c /cad secondary development create arc (4)
Tool grass welfare post
php中删除指定文件夹下的内容
SQL injection closure judgment
ORACLE 11G利用 ORDS+pljson来实现json_table 效果
ORACLE EBS DATAGUARD 搭建
Cve-2015-1635 (ms15-034) Remote Code Execution Vulnerability recurrence
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
2021-07-19c CAD secondary development creates multiple line segments