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.

why you say it doesn't work. I have run it like this:

Enter the number of values to be added to the list
4
Input the values
4
3
2
1
[1, 2, 3, 4]
[4, 3, 2, 1]

As you can see the array 'a' is sorted ([1, 2, 3, 4]) , whereas the 'myList' is not [4, 3, 2, 1].
what were you expecting?

I need to use the binary search on the sorted list, not the original list "mylist". I need to serch for a value in the sorted list. Try mixing up the numbers to really see the problem. Thanks for the reply.

I know that the sort works properly, my problem lies with the binarySearch.

Well this is what I got with mixed numbers:

[1, 2, 3, 4]
[4, 3, 2, 1]
Enter the value you want to search for
4
The value you searched for is at index -5

It should have returned '3'.

After searching the API for collections
found that the method you are using :
int binarySearch(List<? extends Comparable<? super T>> list, T key)
requires a list that implements the "comparable" interface. so when I tried this I got an error: int where = Collections.binarySearch(Arrays.asList(a), searchValue); Because Arrays.asList(a) returns an Object list that doesn't implement the Comparable.
But there is another method you could use if it is not a limitation:
int binarySearch(List<? extends T> list,
T key,
Comparator<? super T> c)

Instead of implementing the Comparator, the API says that if you enter null: A null value indicates that the elements' natural ordering should be used. So it will use the comparator of the Integer class.
so try this:

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);

		int where = Collections.binarySearch(Arrays.asList(a), searchValue, null);


		System.out.println("The value you searched for is at index " + where);
commented: Big help. Thanks. +1

That worked. Thanks a lot for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.