Sunday, August 25, 2013

array

import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;


public class ArraysExample {
       public static void main(String []args)
       {
              int[] nums = {17, 90, 21, 78, 12, 36, 93, 23, 43, 120, 125,64};
              System.out.println("Before Sorting ");
              System.out.println(Arrays.toString(nums));
              Arrays.sort(nums);
              System.out.println("After Sorting ");
              System.out.println(Arrays.toString(nums));
              System.out.print("Enter a value to perform the binary search in the array : ");
              Scanner in = new Scanner(System.in);
              try
              {
                     int position = Arrays.binarySearch(nums, in.nextInt());
                     if(position >= 0)
                     {
                     System.out.println("The entered value found at the position " +  position);
                     }
                     else
                     {
                           System.out.println("The entered value doesn't exists in the array");
                     }
              }
              catch (InputMismatchException e) {
                     System.out.println("Entered value is not a number");
              }
              List<String> list = Arrays.asList("C", "C++", "Java", ".Net", "PHP", "JavaScript", "HTML");
              for(String language : list)
              {
                     System.out.println(language);
              }
              System.out.println(Arrays.toString(Arrays.copyOf(nums, 30)));
       }
}


No comments:

Post a Comment