简体   繁体   中英

How to find a value in arraylist

Is there a method for me to retrieve the value 4500? I know i can use collection.max or collection.min for highest and lowest but how do i get the value in between? thanks

int value = 4000;

for(int i = 0; i < Array.size();i++){
        Array.get(i);

    if (value < Array.get(i) && value <= Array.get(i))
    {
        value=????;


    }

Array.add(1000);
Array.add(2000);
Array.add(3000);
Array.add(4500);    
Array.add(5000);
Array.add(8000);

If the data is sorted do a BinarySearch otherwise do a LinearSearch

eg

Collections.binarySearch(array,value);

Linear search:

int size = array.size();
for(int i=0;i<size;i++){
   if(array.get(i) >= value){       
      if ( i + 1 < size){
         return array.get(i+1);  
      }
      else {
         // No element found                    
      }
}      

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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