在 Java 中遍历链表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4462256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterate through Linked List in Java
提问by ptikobj
Say, I have two references to an Object in a LinkedList List1:
比如说,我有两个对 LinkedList List1 中的对象的引用:
LinkedList<Object> List1 = new LinkedList<Object>();
Object first;
Object last;
I don't want to use the list index of these objects to refer to them, because the length of my list changes. I think this would not work. Now I want to iterate through the sublist defined by first and last, where first defines the beginning of the sublist in List1 and last defines the end of the sublist in List1.
我不想使用这些对象的列表索引来引用它们,因为我的列表长度会发生变化。我认为这行不通。现在我想遍历由 first 和 last 定义的子列表,其中 first 定义了 List1 中子列表的开头,last 定义了 List1 中子列表的结尾。
My problem now is that AFAIK I can't do something like
我现在的问题是 AFAIK 我不能做类似的事情
while (current != last){
// do something
current = someiterator.next();
}
because I'm comparing two objects that in general will point to different locations. Furthermore, I also can't compare the references by their value, because the list may have one value appearing several times. So how can I iterate through this sublist of List1?
因为我正在比较两个通常会指向不同位置的对象。此外,我也无法按引用的值进行比较,因为列表中可能有一个值出现多次。那么如何遍历 List1 的这个子列表呢?
回答by aioobe
You could use something like
你可以使用类似的东西
list1.sublist(list1.indexOf(first), list1.indexOf(last))
Ok, I think I understand your question better now. The above method will use the .equals
method and thus not compare references. Here is probably a better solution for you:
好的,我想我现在更理解你的问题了。上述方法将使用.equals
方法,因此不会比较引用。这可能是一个更好的解决方案:
import java.util.*;
public class Test {
public static void main(String[] args) {
String first = "beta";
String last = "delta";
List<String> list1 = new LinkedList<String>();
list1.add("alpha");
list1.add(first);
list1.add("gamma");
list1.add(last);
list1.add("epsilon");
boolean firstFound = false;
for (String s : list1) {
if (firstFound || (firstFound = s == first))
System.out.println(s);
if (s == last)
break;
}
}
}
回答by Michael Borgwardt
No, your comparison while (current != last)
will work fine. In Java, objects live on the heap and you only work with references. Comparing two references using ==
returns true
iff they refer to the same object, which seems to be exactly what you want.
不,您的比较while (current != last)
会正常工作。在 Java 中,对象存在于堆中,您只能使用引用。使用==
返回值比较两个引用,true
如果它们引用同一个对象,这似乎正是您想要的。
回答by morja
You should use Object.equals() to compare your objects. If your Objects are real Objects and not primitive or Strings (they equal if their value equals) you should be able to do so:
您应该使用 Object.equals() 来比较您的对象。如果您的对象是真正的对象而不是原始对象或字符串(如果它们的值相等,则它们相等)您应该能够这样做:
boolean start = false;
for(Object o : list){
if(o.equals(first){
start = true;
}else if(o.equals(last)){
break;
}
if(start){
// do something
}
}
Or rather use the answer of aioobe
或者更确切地说使用 aioobe 的答案
回答by perdian
One way is not to add the objects directly, but to create a wrapper, so you will have
一种方法不是直接添加对象,而是创建一个包装器,因此您将拥有
List<WrapperObject<Object>> aList = new LinkedList<WrapperObject<Object>>();
Now you can verify the equality of the entriesby checking the wrappers instead of the wrapped objects.
现在您可以通过检查包装器而不是包装的对象来验证条目的相等性。
回答by Olivier Croisier
If you cannot rely neither on == nor .equals(), I don't see how you could possibly define a sublist...
如果您既不能依赖 == 也不能依赖 .equals(),我看不出您如何定义子列表...