简体   繁体   English

Java在arraylist中查找值

[英]Java Finding a value in arraylist

I have added few number into the arraylist . 我在arraylist中添加了一些数字。 I would want to find the certain value from it.Example i have 4,4,9,9,18. 我想从中找到某个值。示例我有4,4,9,9,18。 I would like to find the value of 26. If 26 > largest value in the list it will display 18 and if value is 17 it will display 9, and if value is 5 it will display 4. Also is there another method to implement this search because liner search might be slow. 我想找到26的值。如果列表中26>最大值,它将显示18,如果value是17,它将显示9,如果value是5,它将显示4。此外,还有另一种方法可以实现这一点。搜索,因为班轮搜索可能很慢。

search value 26

    [4,4,9,9,18] display 18
    [20,20,29,29,4] display 20
    [28,28,28,1,10] display 28

if you have this list and search 26, it will output the first element. 如果您有此列表并搜索26,它将输出第一个元素。 because the first element is <= than the value being search. 因为第一个元素比要搜索的值<=。

but current output is 但目前的输出是

Value of value2 : 9 值2的值:9

    public class Arraylist {

    public static ArrayList<Integer> aList;

    public static void main(String[] args) {
        aList = new ArrayList<Integer>();
        aList.add(4);
        aList.add(4);
        aList.add(9);
        aList.add(9);
        aList.add(18);
        int value = 26;
        int value2 = 0;

        for (int i = 0; i < aList.size(); i++) {
            if (aList.get(i) <= value) {          
                if (i + 1 < aList.size()) {
                    value2 = aList.get(i);
                } else if(i > aList.size()) {
                    value2 = aList.get(i);

                }
            }
        }
        System.out.println("Value of value2 : " + value2);
    }
}

I have written the code using an array. 我已经使用数组编写了代码。 You can easily adopt it to ArrayList 您可以轻松地将其应用于ArrayList

int a[] = {28,28,28,1,10};
// int a[] = {20,20,29,29,4}; // other input of yours
// int a[] = {4,4,9,9,18};  

   int x = 26;

   int liVal = -1;
   for(int i=0; i<a.length;i++)
       if(x < a[i]) // if we met a value > x
       {
          if(liVal==-1) // if we could not find any largest value smaller than x
              liVal = a[i]; // return the value > x
          break;
       }
       else if(x > a[i]) // find the largest value smaller than x, 
       {
           if(liVal < a[i])
               liVal = a[i];
       }

System.out.println(liVal);

A trivial and un-optimized version: 琐碎且未经优化的版本:

int value = 26 // or whatever parameter you get
int retVal = Integer.MIN_VALUE;
for (int i : list) {
  if (i <= value && i > retVal) {
    retVal = i;
  }
}
return retVal;

If the array is sorted you can use a variant of binary search. 如果数组已排序,则可以使用二进制搜索的变体。
see http://en.wikipedia.org/wiki/Binary_search_algorithm 参见http://en.wikipedia.org/wiki/Binary_search_algorithm

If I understand correctly, you want to find the biggest number in your array that is smaller or equal to value . 如果我理解正确,则想在数组中找到小于或等于value的最大数字。 I would do it like this: 我会这样做:

for (int i = 0; i < aList.size(); i++) {
    if ( aList.get(i) <= value && aList.get(i) > value2) {
        value2 = aList.get(i);
    }
}

Also in your example, you do value2 = 0 . 同样在您的示例中,您执行value2 = 0 This is ok if you can guarante that the array only contains positive values. 如果可以保证数组仅包含正值,则可以。 Otherwise it would be better to use value2 = Integer.MIN_VALUE . 否则,最好使用value2 = Integer.MIN_VALUE

Finally, this code assume that the array is not guarantied to be sorted and that you will only need to search it once. 最后,此代码假定不保证对数组进行排序,并且只需要搜索一次即可。 Otherwise, a binary search could be more performant. 否则, 二进制搜索可能会更有效。 Other answers on this question already show how to accomplish that. 关于这个问题的其他答案已经表明了如何实现这一目标。

Once you sort the list, binarySearch in Collections will do the trick: 对列表进行排序后, Collections binarySearch将完成操作:

Collections.sort(aList)
int index = Collections.binarySearch(aList)

If index is nonnegative, the number was found in the list, and index is the position. 如果index为非负数,则在列表中找到该数字,而index为位置。 If it is negative, it wasn't found, but index indicates where it would be if it were in the list. 如果它是负数,则找不到它,但是index指示它在列表中的位置。

And with O(log n) runtime for the search. 并使用O(log n)运行时进行搜索。

According to the OP's comments: 根据OP的评论:

  1. The list isn't sorted 列表未排序
  2. if value < min return min 如果值<min返回min
  3. if value > max return max 如果值>最大值返回最大值
  4. if min <= value <= max return nearest val <= value 如果min <=值<= max返回最近的val <=值

     public static int findValue(List<Integer> list, int value){ int min = Integer.MAX_VALUE, nearest = Integer.MIN_VALUE; for(Integer v : list){ if(v == value) return value; if(v > nearest && v < value) nearest = v; if(v < min) min = v; } return value < min ? min : nearest; } 

As a side note, you don't need to keep track of the max because nearest = max if value > max(list). 附带说明一下,您不需要跟踪最大值,因为如果value> max(list),则最近=最大值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM