Hello friends. I'm working on a program to sort a list object using toArray, Arrays.sort, and Arrays.asList. The problem asks that I then apply Collections.binarySearch directly to the list object. I know what the problem is (which I have stated through comments in my code) but I don't know how to resolve it.
Here is my code :
import java.util.*;
//begin class PartC
public class PartC
{
//begin main method
public static void main(String args[])
{
//construct ArrayList object
ArrayList<Integer> mylist = new ArrayList<Integer>();
//ArrayList<Integer> newlist = new ArrayList<Integer>(mylist);
//initialize scanner
Scanner scan = new Scanner(System.in);
//ask user how long the list will be
System.out.println("Enter the number of values to be added to the list");
int nums = scan.nextInt();
//ask user to input each value separated by the enter key
System.out.println("Input the values");
//add all values to the list
for(int index = 0; index<nums; ++index)
{
int input = scan.nextInt();
mylist.add(input);
}
Object[] a = mylist.toArray();
Arrays.sort(a);
System.out.println(Arrays.asList(a));
//not working because mylist is not sorted in ascending order
//must make sorted list the parameter for the Collections.binarySearch
System.out.println(mylist);
System.out.println("Enter the value you want to search for");
int searchValue = scan.nextInt();
int where = Collections.binarySearch(mylist, searchValue);
System.out.println("The value you searched for is at index " + where);
}//terminates main method
}//terminates class PartC
Thanks for all your help.