package com.pack.datastructure;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class ArrayListDemo {
public static void main(String[] args) {
int i;
ArrayList<Integer> arr=new <Integer>ArrayList();
System.out.println("Enter the size for arraylist");
Scanner sc=new Scanner(System.in);
int size=sc.nextInt();
System.out.println("Enter the Elements to be sorted");
for(i=0;i<size;i++)
{
arr.add(sc.nextInt());
}
System.out.println("Original List is :");
for(i=0;i<size;i++)
{
System.out.print(arr.get(i)+" ");
}
Collections.sort(arr);
System.out.println("Sorted List is :");
for(i=0;i<size;i++)
{
System.out.print(arr.get(i)+" ");
}
System.out.println("Enter the element to search");
int choice=sc.nextInt();
int pos=Collections.binarySearch(arr, choice);
if(pos>0)
System.out.println("Element found at "+pos);
else
System.out.println("Element not found");
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class ArrayListDemo {
public static void main(String[] args) {
int i;
ArrayList<Integer> arr=new <Integer>ArrayList();
System.out.println("Enter the size for arraylist");
Scanner sc=new Scanner(System.in);
int size=sc.nextInt();
System.out.println("Enter the Elements to be sorted");
for(i=0;i<size;i++)
{
arr.add(sc.nextInt());
}
System.out.println("Original List is :");
for(i=0;i<size;i++)
{
System.out.print(arr.get(i)+" ");
}
Collections.sort(arr);
System.out.println("Sorted List is :");
for(i=0;i<size;i++)
{
System.out.print(arr.get(i)+" ");
}
System.out.println("Enter the element to search");
int choice=sc.nextInt();
int pos=Collections.binarySearch(arr, choice);
if(pos>0)
System.out.println("Element found at "+pos);
else
System.out.println("Element not found");
}
}