java container: 15, Iterator how to use? What are the characteristics?

Iterator how to use? What are the characteristics?

Iterator how to use (usage)?

(1) Iterator () Returns a container requires Iterator. Iterator will be ready to return the first element of the sequence.
(2) using the next () to get the next element in the sequence
(3) hasNext () to check whether there are elements in the sequence.
(4) using remove () will return the iterator element of the newly deleted.

		List<String> list = new ArrayList<String>();
		list.add("张三");
		list.add("李四");
		list.add("王五");
		list.add("赵六");

		Iterator<String> it = list.iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
			if (it.next().equals("李四")) {
				it.remove();
			}
		}
		System.out.println("------------------------");
		System.out.println(list);

Iterator features?

Process (1) Iterator through the collection element's not allowed to modify the thread collection element , otherwise it will throw ConcurrentModificationEception exception.
Process (2) Iterator to traverse the set of elements can be removed remove method elements in the set, one object is deleted by Iterator.next () method returns.
(3) Iterator must be attached to a collection class object exists, Iterator itself does not have a function to load data objects.
(. 4) Next () method, which returns an Iterator next element pointed to by the cursor form .

Related Recommendation: the Java container --Iterator detailed analysis of iterators

Published 57 original articles · won praise 13 · views 1103

Guess you like

Origin blog.csdn.net/weixin_42924812/article/details/105054514