Search a java.util.List starting at a specific index

十年热恋 提交于 2021-01-27 22:31:25

问题


Is there a built-in method to search a java.util.List specifying the first item to start the search from? Like you can do with Strings

I know I can easily implement something on my own, but I'd rather not reinvent the wheel if Java or http://commons.apache.org/collections/api-release/org/apache/commons/collections/package-summary.html already has it.

I'm not asking how to implement this, I'm asking whether something is already available A lot of the suggestions here were buggy.

If anybody cares for receiving credit for the right answer, please update your answer to say that there is not a built-in way to do it (if you know for sure)

Here's what I would like to do

List<String> strings = new ArrayList<String>();
// Add some values to the list here
// Search starting from the 6th item in the list
strings.indexOf("someValue", 5);

Right now I'm using

/**
 * This is like List.indexOf(), except that it allows you to specify the index to start the search from
 */
public static int indexOf(List<?> list, Object toFind, int startingIndex) {
    for (int index = startingIndex; index < list.size(); index++) {
        Object current = list.get(index);
        if (current != null && current.equals(toFind)) {
            return index;
        }
    }
    return -1;
}

And I've also implemented it as

public static int indexOf(List<?> list, Object toFind, int startingIndex) {
    int index = list.subList(startingIndex).indexOf(toFind);
    return index == -1 ? index : index + startingIndex;
}

回答1:


No not a single method, but there is a simple documented way of doing this with 1-2 lines of code. It even says so in the documentation for this method:

strings.subList(5, strings.size()).indexOf("someValue");

Possibly add 5 to the result (if not -1), depending on if you want to keep that sublist around or not etc:

int result = list.subList(startIndex, list.size()).indexOf(someValue);
return result== -1 ? -1 : result+startIndex;

Note: subList does not create a new List, just a view into the original one.




回答2:


You can use sublist, like this:

List<String> strings = new ArrayList<String>();
// Add values ...
int start = 5;
int pos = strings.sublist(start, strings.size()).indexOf("someValue");
// Don't forget to add the starting point back
if (pos >= 0) pos += start;



回答3:


For a more generic approach, try this method:

public static int indexOf(List<?> list, int start, Object value) {
    int idx = list.subList(start, list.size()).indexOf(value);
    return idx != -1 ? idx + start : -1;
}

It will work for lists of any type, returning -1 if no element was found. Use it like this:

List<String> strings = Arrays.asList("a", "b", "c", "a", "b", "c");
int idx = indexOf(strings, 2, "a");
System.out.println(idx);
> 3



回答4:


You can use a combination of subList(int from, int to) and indexOf, like that:

int pos = strings.subList(5, strings.size()).indexOf("someValue");
if (pos >= 0) {
    pos += 5;
}


来源:https://stackoverflow.com/questions/10938364/search-a-java-util-list-starting-at-a-specific-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!