Java 列表:从标识符中获取下一个或上一个元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6411542/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:43:55  来源:igfitidea点击:

Java list : get next or previous element from an identifier

javalistpointers

提问by BasicCoder

I want to navigate into a list by identifier.

我想通过 identifier 导航到列表中

1- I manage/create a list.

1- 我管理/创建一个列表。

2- I create function to get next item of a identifier element from my list

2- 我创建函数以从我的列表中获取标识符元素的下一项

Can you help me to fix this code?

你能帮我修复这个代码吗?

Prepare the list

准备清单

List<String> myList = new ArrayList<String>();
myList.add("1");
myList.add("2");
myList.add("3");
myList.add("4");
myList.add("5");


public String function getNext(String uid) {

    if (myList.indexOf(uid).hasNext()) {
        return myList.indexOf(uid).nextElement();
    }
    return "";
}

public String function getPrevious(String uid) {
    return myList.indexOf(uid).hasPrevious() ? myList.indexOf(uid).previousElement() : "";
}

采纳答案by Peter Lawrey

You could use an index to lookup your String which is faster and simpler however to implement the functions as you have them.

您可以使用索引来查找您的字符串,这更快更简单,但是可以按照您的要求实现功能。

public String getNext(String uid) {
    int idx = myList.indexOf(uid);
    if (idx < 0 || idx+1 == myList.size()) return "";
    return myList.get(idx + 1);
}

public String getPrevious(String uid) {
    int idx = myList.indexOf(uid);
    if (idx <= 0) return "";
    return myList.get(idx - 1);
}

Using a List.get(i)is O(1)which makes keeping the index the fastest option. List.indexOf(String)is O(n). Using a NavigatbleSet might appear attractive as it is O(log n), however the cost of creating an object is so high that the collection has to be fairly large before you would see a benefit. (In which case you would use the first option)

使用 a List.get(i)isO(1)这使得保持索引成为最快的选择。List.indexOf(String)O(n)。使用 NavigatbleSet 可能看起来很有吸引力O(log n),但是创建对象的成本如此之高,以至于集合必须相当大才能看到好处。(在这种情况下,您将使用第一个选项)

回答by fortran

If your elements are not repeated, what you need is a NavigableSet:

如果你的元素不重复,你需要的是一个 NavigableSet:

http://download.oracle.com/javase/6/docs/api/java/util/NavigableSet.html

http://download.oracle.com/javase/6/docs/api/java/util/NavigableSet.html

The methods higherand lowerare what you are looking for.

方法higherlower就是你正在寻找的。

回答by RoToRa

Lists don't have a nextElement()method. indexOfreturns the integer index of the item. You could simply add (or subtract) one to get the next (or previous) item:

列表没有nextElement()方法。indexOf返回项目的整数索引。您可以简单地添加(或减去)一个以获得下一个(或上一个)项目:

public String function getNext(String uid) {
   var index = myList.indexOf(uid);
   if (index > -1) {
     try {
       return myList.get(i+1);
     } catch ( IndexOutOfBoundsException e) {
       // Ignore
     }
   }
   return ""; // consider returning `null`. It's usually a better choice.
}

However looking up an object with indexOfon ArrayListis a very slow process, because it has to check every single entry. There are better ways to this, but that depends on what you are actually trying to achieve.

然而,使用indexOfon查找对象ArrayList是一个非常缓慢的过程,因为它必须检查每个条目。有更好的方法可以做到这一点,但这取决于您实际尝试实现的目标。