Reputation: 878
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);
Upvotes: 0
Views: 94
Reputation: 18460
If the data is sorted do a BinarySearch
otherwise do a LinearSearch
e.g.
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
}
}
Upvotes: 1