検索を開始する最初の項目を指定するjava.util.Listを検索するための組み込みメソッドはありますか?文字列でできるように
自分で簡単に何かを実装できることはわかっていますが、Javaまたはhttp://commons.apache.org/collections/api-release/org/apache/commons/collections/package-summaryの場合は、車輪の再発明をしたくありません。 .htmlにはすでにそれがあります。
私はこれをどのように実装するかを尋ねているのではなく、何かがすでに利用可能かどうかを尋ねています。ここでの提案の多くはバグがありました。
誰かが正しい答えのクレジットを受け取ることを気にかけている場合は、答えを更新して、それを行うための組み込みの方法がないことを伝えてください(確かに知っている場合)
これが私がやりたいことです
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);
今使っています
/**
* 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;
}
そして私はそれを次のように実装しました
public static int indexOf(List<?> list, Object toFind, int startingIndex) {
int index = list.subList(startingIndex).indexOf(toFind);
return index == -1 ? index : index + startingIndex;
}