Vote Up 0 Vote Down

Binary Search element in a list

We would like to know how to binary Search element in a list.

answer


Vote Up 0 Vote Down
import java.text.DateFormatSymbols;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    List<String> list = new LinkedList<String>();

    list.add("a");
    list.add("b");
    list.add("c");
    list.add("d");
    list.add("e");
    list.add("f");


    Collections.sort(list);
    System.out.println(list);
    int index = Collections.binarySearch(list, "c");
    if (index > 0) {
      System.out.println("Found at index = " + index);
      String month = (String) list.get(index);
      System.out.println(month);
    }
  }
}
flag | link

your answer

Who Are You?

Have an account already? Login before answering!