First, the iterator Iterator interface
1 Overview
- Iterator: Iterator
- To carry out collection iterator
- It depends on the collection exists
2, Iterator interface member method
boolean hasNext()
E next()
package liebiao; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class ListDemo { public static void main(String[] args){ List l = new ArrayList();//创建集合对象 l.add("苍井空"); l.add("是"); l.add("一个"); l.add("漂亮的"); l.add("女孩"); System.out.println(l); System.out.println("---------------------"); //遍历集合 Iterator it = l.iterator(); //System.out.println(it); while (it.hasNext()){//使用hasNext()检查序列中是否还有元素。 String s = (String)it.next();//使用next()获得序列中的下一个元素。 System.out.println(s); } } }
3, using the Iterator interface
Case: a package class Students
package liebiao; public class Students { private String name; private int age; public Students(){//无参构造函数 super(); } public Students(String name,int age){ super(); this.name = name; this.age = age; } // 成员方法 // getXxx()/setXxx() //Alt + Insert(idea 工具) public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
Store custom objects, and traverse
package liebiao; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class ListDemo{ public static void main(String[] args){ List l = new ArrayList();//创建集合对象 Students s1 = new Students("苍井空",18); Students s2 = new Students("波多野结衣",19); Students s3 = new Students("泷泽萝拉",20); Students s4 = new Students("水菜丽",21); l.add(s1); l.add(s2); l.add(s3); l.add(s4); //遍历集合 Iterator it = l.iterator(); while (it.hasNext()){ Students s = (Students) it.next(); System.out.println("姓名:"+s.getName()+"----年龄:"+s.getAge()); } } }
4, the principle Iterator interface
Second, the interface sequence List
1 Overview
- List: Sequence
- List interface is a subclass of Collection, is an abstract class
- List interface may control the insertion position of each element in the list of access to the element according to the index element
- Unlike a Set, List list allows duplicate elements
2, List Case
-
Storage string and ergodic
-
Store custom objects and traverse
3, members of the List interface method
void add(int index,E element)
E remove(int index)
E get(int index)
E set(int index,E element)
ListIterator listIterator()
package liebiao; import java.util.ArrayList; import java.util.Iterator; import java.util.List; class ListDemo{ public static void main(String[] args){ List l = new ArrayList(); l.add("hello"); l.add("java"); l.add("study"); //指定位置添加元素 l.add(1,"你好"); //获取指定位置的元素 System.out.println(l.get(3)); System.out.println("--------------------"); //根据索引删除元素 System.out.println(l.remove(2)); System.out.println("======================="); //根据索引修改元素 System.out.println(l.set(0,"苍井空")); System.out.println(l); Iterator it = l.iterator(); while (it.hasNext()){ String s = (String)it.next(); System.out.println(s); } } }
Three, ListIterator Interface
1 Overview
- ListIterator interface is a subclass of List interface
2, members of the interface method ListIterator
boolean hasPrevious()
E previous()
ConcurrentModificationException
(Parallel modify exceptions)
3, the list iterator
package liebiao; import java.util.ArrayList; import java.util.List; import java.util.ListIterator; class ListDemo{ public static void main(String[] args){ List l = new ArrayList();//创建List 集合对象 l.add("hello"); l.add("java"); ListIterator it = l.listIterator(); while (it.hasNext()){ String s = (String)it.next(); System.out.println(s); } System.out.println("------------------------"); System.out.println(it.previous());//获取上一个元素 System.out.println("=========================="); while (it.hasPrevious()){//是否有上一个元素 String s = (String)it.previous(); System.out.println(s); } } }