問題描述
我剛剛了解了 Java 集合框架如何在鏈表中實現數據結構.據我了解, Iterators
是一種遍歷數據結構(如列表)中的項目的方法.為什么要使用這個接口?為什么 hasNext()
、next()
和 remove()
方法不直接編碼到數據結構實現本身?
I just learned about how the Java Collections Framework implements data structures in linked lists. From what I understand, Iterators
are a way of traversing through the items in a data structure such as a list. Why is this interface used? Why are the methods hasNext()
, next()
and remove()
not directly coded to the data structure implementation itself?
來自 Java 網站:鏈接文本
From the Java website: link text
公共接口迭代器 An集合上的迭代器.迭代器代替枚舉中的Java 集合框架.迭代器在兩個方面不同于枚舉: public interface Iterator<E> An
iterator over a collection. Iterator
takes the place of Enumeration in the
Java collections framework. Iterators
differ from enumerations in two ways:
- Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.
- Method names
have been improved.
我嘗試用谷歌搜索,但似乎找不到明確的答案.有人能解釋一下 Sun 選擇使用它們的原因嗎?是因為更好的設計嗎?提高安全性?好的 OO 實踐?
I tried googling around and can't seem to find a definite answer. Can someone shed some light on why Sun chose to use them? Is it because of better design? Increased security? Good OO practice?
任何幫助將不勝感激.謝謝.
Any help will be greatly appreciated. Thanks.
推薦答案
為什么要用這個接口?
Why is this interface used?
因為它支持允許客戶端程序員迭代任何類型的集合的基本操作(注意:不一定是
Object
意義上的Collection
).Because it supports the basic operations that would allow a client programmer to iterate over any kind of collection (note: not necessarily a
Collection
in theObject
sense).為什么方法...不是直接的編碼到數據結構實現本身?
Why are the methods... not directly coded to the data structure implementation itself?
他們是,他們只是被標記為私人,所以你不能接觸他們并與他們混為一談.更具體地說:
They are, they're just marked Private so you can't reach into them and muck with them. More specifically:
- 您可以實現
Iterator
或對其進行子類化,這樣它就可以做一些標準的不做的事情,而不必改變它所迭代的實際對象. - 可以遍歷的對象不需要讓它們的接口被遍歷方法弄得一團糟,尤其是任何高度專業化的方法.
- 您可以將
Iterators
分發給任意數量的客戶端,每個客戶端都可以按照自己的速度以自己的時間遍歷. 特別是 java.util 包中的 - Java
Iterators
如果支持它們的存儲在您仍然有Iterator
輸出的情況下被修改,則會引發異常.此異常讓您知道Iterator
現在可能正在返回無效對象.
- You can implement or subclass an
Iterator
such that it does something the standard ones don't do, without having to alter the actual object it iterates over. - Objects that can be traversed over don't need to have their interfaces cluttered up with traversal methods, in particular any highly specialized methods.
- You can hand out
Iterators
to however many clients you wish, and each client may traverse in their own time, at their own speed. - Java
Iterators
from the java.util package in particular will throw an exception if the storage that backs them is modified while you still have anIterator
out. This exception lets you know that theIterator
may now be returning invalid objects.
對于簡單的程序,這似乎都不值得.不過,使它們有用的那種復雜性很快就會出現.
For simple programs, none of this probably seems worthwhile. The kind of complexity that makes them useful will come up on you quickly, though.
這篇關于Java中的Iterator接口有什么好處?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持! - 您可以實現