java Java搜索-Arraylist

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

Java search-Arraylist

java

提问by rahul

ArrayList searchList = new ArrayList();
ArrayList words=(ArrayList) request.getSession().getAttribute("words");
words.add("one");
words.add("twenty one");
words.add("thirty one");
words.add("two");
words.add("twenty two");
words.add("thirty two");
words.add("three");
words.add("twenty three");
words.add("thirty three");'

If I have this arraylist and I want to search all the strings containing one(i.e. one,twenty one and thirty one), what logic should I use? Means how should I do that?

如果我有这个数组列表并且我想搜索所有包含一个(即一、二十一和三十一)的字符串,我应该使用什么逻辑?意思是我该怎么做?

回答by nfechner

for (String item : searchList) {
    if (item.contains("one") {
        // Do something. Like adding the result to a different list.
        // If you need the index from the original list, you a for instead of a for each
    }
 }

回答by Petar Minchev

for (String word : words) {
   if (word.contains("one")) {
       //we have a match
   }
}

回答by Jigar Joshi

//iterate through words
for(String str : list){
  //check if word contains the key
  if(str.contains(key)){ 
     //add its reference to another resultant list
     result.add(str);
  }
}

回答by PeterMmm

Of course you have to loop thru the elements. Look for ways to loop thru an ArrayList: that can be indexed or with the

当然,您必须遍历元素。寻找循环遍历 ArrayList 的方法:可以索引或使用

for (x : collect) 

notation.

符号。

In the loop you have to do some pattern matching. Read String Java API doc for a method.

在循环中,您必须进行一些模式匹配。阅读 String Java API 文档以获取方法。

(Give'em some think food ...)

(给他们一些思考的食物......)

回答by Damian Leszczyński - Vash

You could solve this using iterators if the condition will be more complex

如果条件更复杂,您可以使用迭代器解决这个问题

    public interface IPredicate<T> {

        boolean check(T t);

    }


public class PredicatIterable<T> implements Iterable<T> {

        private final Iterator<T> iterator;
        private final IPredicate<T> predicate;

        public PredicatIterable(Iterable<T> iterable, IPredicate<T> predicate) {
            this.iterator = iterable.iterator();
            this.predicate = predicate;
        }

        @Override
        public Iterator<T> iterator() {
            return new Iterator<T>() {

                T current;

                @Override
                public boolean hasNext() {

                    if(iterator.hasNext()) {
                        T next = iterator.next();

                        if(predicate.check(next)) {
                            current = next;
                            return true;
                        } 
                        current = null;
                    }

                    return false;
                }

                @Override
                public T next() {
                    return current;
                }

                @Override
                public void remove() {
                    throw new RuntimeException("Invalid useage of method");
                }



            };
        }

    }

To validate more the single predicate you can create also method that is responsible for conuntion or alternative of two IPredicate argument.

要验证更多的单个谓词,您还可以创建负责连接或替代两个 IPredicate 参数的方法。

回答by Youness

In general, when searching an item in a List, the best solution is to sort your Listfirst using Collections.sort()method. Then using the Collections.binarySearch()method, find your element. In this case your elements are Stringtype that are Comparableand can be sorted alphabetically otherwise you needed to implement Comparableinterface for your element class type.

通常,在 a 中搜索项目时List,最好的解决方案是对您的List第一个 usingCollections.sort()方法进行排序。然后使用该Collections.binarySearch()方法,找到您的元素。在这种情况下,您的元素是可以按字母顺序排序的String类型,Comparable否则您需要Comparable为元素类类型实现接口。