首页 / JAVA / java Iterator 的用法
java Iterator 的用法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java Iterator 的用法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3558字,纯文字阅读大概需要6分钟。
内容图文
java.util package has public interface Iterator and contains three methods:
- boolean hasNext(): It returns true if Iterator has more element to iterate.
- Object next(): It returns the next element in the collection until the hasNext()method return true. This method throws ‘NoSuchElementException’ if there is no next element.
- void remove(): It removes the current element in the collection. This method throws ‘IllegalStateException’ if this function is called before next( ) is invoked.
// Java code to illustrate the use of iterator
import
java.io.*;
import
java.util.*;
class
Test {
public
static
void
main(String[] args)
{
ArrayList<String> list =
new
ArrayList<String>();
list.add(
"A"
);
list.add(
"B"
);
list.add(
"C"
);
list.add(
"D"
);
list.add(
"E"
);
// Iterator to traverse the list
Iterator iterator = list.iterator();
System.out.println(
"List elements : "
);
while
(iterator.hasNext())
System.out.print(iterator.next() +
" "
);
System.out.println();
}
}
|
ListIterator
‘ListIterator’ in Java is an Iterator which allows users to traverse Collection in both direction. It contains the following methods:
- void add(Object object): It inserts object immediately before the element that is returned by the next( ) function.
- boolean hasNext( ): It returns true if the list has a next element.
- boolean hasPrevious( ): It returns true if the list has a previous element.
- Object next( ): It returns the next element of the list. It throws ‘NoSuchElementException’ if there is no next element in the list.
- Object previous( ): It returns the previous element of the list. It throws ‘NoSuchElementException’ if there is no previous element.
- void remove( ): It removes the current element from the list. It throws ‘IllegalStateException’ if this function is called before next( ) or previous( ) is invoked.
// Java code to illustrate the use of ListIterator
import
java.io.*;
import
java.util.*;
class
Test {
public
static
void
main(String[] args)
{
ArrayList<String> list =
new
ArrayList<String>();
list.add(
"A"
);
list.add(
"B"
);
list.add(
"C"
);
list.add(
"D"
);
list.add(
"E"
);
// ListIterator to traverse the list
ListIterator iterator = list.listIterator();
// Traversing the list in forward direction
System.out.println(
"Displaying list elements in forward direction : "
);
while
(iterator.hasNext())
System.out.print(iterator.next() +
" "
);
System.out.println();
// Traversing the list in backward direction
System.out.println(
"Displaying list elements in backward direction : "
);
while
(iterator.hasPrevious())
System.out.print(iterator.previous() +
" "
);
System.out.println();
}
}
|
详见https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
// Java code to illustrate the use of ListIterator
import
java.io.*;
import
java.util.*;
class
Test {
public
static
void
main(String[] args)
{
ArrayList<String> list =
new
ArrayList<String>();
list.add(
"A"
);
list.add(
"B"
);
list.add(
"C"
);
list.add(
"D"
);
list.add(
"E"
);
// ListIterator to traverse the list
ListIterator iterator = list.listIterator();
// Traversing the list in forward direction
System.out.println(
"Displaying list elements in forward direction : "
);
while
(iterator.hasNext())
System.out.print(iterator.next() +
" "
);
System.out.println();
// Traversing the list in backward direction
System.out.println(
"Displaying list elements in backward direction : "
);
while
(iterator.hasPrevious())
System.out.print(iterator.previous() +
" "
);
System.out.println();
}
}
|
原文:https://www.cnblogs.com/jiml/p/9357021.html
内容总结
以上是互联网集市为您收集整理的java Iterator 的用法全部内容,希望文章能够帮你解决java Iterator 的用法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。