Java Finding a value in arraylist

非 Y 不嫁゛ 提交于 2020-01-14 06:57:08

问题


I have added few number into the arraylist . I would want to find the certain value from it.Example i have 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.

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. because the first element is <= than the value being search.

but current output is

Value of value2 : 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);
    }
}

回答1:


I have written the code using an array. You can easily adopt it to 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);



回答2:


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;



回答3:


If the array is sorted you can use a variant of binary search.
see http://en.wikipedia.org/wiki/Binary_search_algorithm




回答4:


Once you sort the list, binarySearch in Collections will do the trick:

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

If index is nonnegative, the number was found in the list, and index is the position. If it is negative, it wasn't found, but index indicates where it would be if it were in the list.

And with O(log n) runtime for the search.




回答5:


If I understand correctly, you want to find the biggest number in your array that is smaller or equal to 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. 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.

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.




回答6:


According to the OP's comments:

  1. The list isn't sorted
  2. if value < min return min
  3. if value > max return max
  4. if min <= value <= max return nearest val <= value

    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).



来源:https://stackoverflow.com/questions/19580855/java-finding-a-value-in-arraylist

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