[英]Converting Linked List to a Circular Linked List in JAVA
有什么方法可以將 LinkedList 類的對象轉換為循環鏈表。
或者在 java.util 中是否有任何預定義的類,例如 CircularLinkedList 類似這樣的東西(比如http://docs.oracle.com/javase/1.4.2/docs/api/java/util/LinkedList.html )
任何幫助將非常感激....
提前致謝 :-)
不,LinkedList 的封裝方式使其無法將其尾部連接到其頭部。 我不認為任何默認集合都支持這一點,因為這樣它就不會再履行Iterable的合同,這表明迭代器必須在某個時候結束。
當您需要這樣的數據結構時,您必須自己實現它。
看看 Guava 的 Iterables.cycle 方法。
public static <T> Iterable<T> cycle(Iterable<T> iterable)
返回一個迭代器,它的迭代器在 iterable 的元素上無限循環。
如果iterable.iterator()
支持,則該迭代器支持 remove()。 調用 remove() 后,后續循環會省略已刪除的元素,該元素不再是可迭代的。 迭代器的hasNext()
方法返回 true,直到 iterable 為空。
請參閱文檔: http : //guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Iterables.html#cycle%28java.lang.Iterable%29 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.