title: Java Finding a value in arraylist
body: <p>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.</p>
  
  <pre><code>search value 26
  
      [4,4,9,9,18] display 18
      [20,20,29,29,4] display 20
      [28,28,28,1,10] display 28
  </code></pre>
  
  <p>if you have this list and search 26, it will output the first element. because the first element is &lt;= than the value being search.</p>
  
  <p>but current output is</p>
  
  <blockquote>
    <p>Value of value2 : 9</p>
  </blockquote>
  
  <pre><code>    public class Arraylist {
  
      public static ArrayList&lt;Integer&gt; aList;
  
      public static void main(String[] args) {
          aList = new ArrayList&lt;Integer&gt;();
          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 &lt; aList.size(); i++) {
              if (aList.get(i) &lt;= value) {          
                  if (i + 1 &lt; aList.size()) {
                      value2 = aList.get(i);
                  } else if(i &gt; aList.size()) {
                      value2 = aList.get(i);
  
                  }
              }
          }
          System.out.println("Value of value2 : " + value2);
      }
  }
  </code></pre>