开发者

search in part of array list - java

it is possible to do a search for example between the position 5 to 10 in a arrayList?

how?

in this case i need to do a search between positi开发者_如何学Pythonon 1-5, 5-10, ... and i want to avoid another lists

EDIT: i have something like that

if (pp.move(i).subList(5, 10).contains(pn50.getText())) {

but i have a problem with this code, i needto call a method like:

if (pp.move(i).suit().subList(5, 10).contains(pn50.getText())) {

but i get an error: The method subList(int, int) is undefined for the type String thanks!!


You can get a sublist of what you need (which isn't an expensive operation) and then search that entire list. Like so:

if (myList.subList(0, 5).contains(objectToFind)) {
   System.out.println("Found object between indexes 0 and 5");
}

UPDATE: TO convert your String of suits into a list first, if the string is comma delimited, then you can do it like so:

String[] suitArray = pp.move(i).suit().split(",");
List myList = List list = Arrays.asList(suitArray);

And then use the first bit of code above to check it. Now, if you are just searching for something in a String, you can probably do it more efficiently by not converting to a List first, but that would be an entirely new question.


Yes. Read the documentation for java.util.List.subList.


is it ok if you grab a sub list ?

ArrayList.subList(fromIndex, toIndex);

This way you have subset of your items, manipulate them and still not having a "new" list (modifying the sublist will modify the original list)


but i get an error: The method subList(int, int) is undefined for the type String thanks!!

That has to be because your suit() method is declared as returning a String.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜