12:51 am - Thursday July 27, 2017

Collections example program forbinarySearch(List> list, T key)

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044
import java.util.List;
public class Bsearch {
 public static void main(String args[])
 {
 String simpsons[] = { "Ball", "House", "Lime", "Moon", "Head", "Mani", "Rabbit" };
 List<String> list = new ArrayList<String>(Arrays.asList(simpsons));//using collections.sort & size
   Collections.sort(list);
   System.out.println("Sorted list: [length: " + list.size() + "]");
   System.out.println(list); // search mani
   int index = Collections.binarySearch(list, "Mani");
   System.out.println("Findoutmani " + index);//Search for element not in list
   index = Collections.binarySearch(list, "Jani");
   System.out.println("not found " + index);
 }
}

Output:
Sorted list: [length: 7]
[Ball, Head, House, Lime, Mani, Moon, Rabbit]
Findoutmani 4
not found -4

BACK


							
							
			
Filed in: Z-A All Java Codes

Comments are closed.