hello
I am getting System.InvalidOperationException while trying to search an object in and ArrayList object. In the following code i am getting System.InvalidOperationException in the lin
int myIndex = myList.BinarySearch(myObject)
import System .*
import System.Collections .*
public class SamplesArrayLis
public static void main(String[] args
// Creates and initializes a new ArrayList
ArrayList myAL = new ArrayList()
for (int i = 0; i <= 4; i++
myAL.Add(new Integer((i * 2)))
// Locates a specific object that does not exist in the ArrayList
Object myObjectOdd = new Integer(3)
FindMyObject(myAL, myObjectOdd)
} //Mai
public static void FindMyObject(ArrayList myList, Object myObject
int myIndex = myList.BinarySearch(myObject)
if (myIndex < 0)
Console.WriteLine("The object to search for ({0}) is not found. The next larger object is at index {1}.", myObject, System.Convert.ToString(~myIndex))
else
Console.WriteLine("The object to search for ({0}) is at index {1}.", myObject, System.Convert.ToString(myIndex))
} //FindMyObjec
} //SamplesArrayList
Can any one help me out
Thanks
Lars-Inge T?nnessen - 17 Mar 2004 12:47 GMT
Looks like the Java types does not implement IComparable. Please use .net
types or a Java list collection. It works with the .net System.String().
import System .* ;
import System.Collections .* ;
public class SamplesArrayList
{
public void FindMyObject(ArrayList myList, Object myObject)
{
int myIndex = myList.BinarySearch(new System.String(""+myObject));
if (myIndex < 0)
{
Console.WriteLine("The object to search for ({0}) is not found.
The next larger object is at index {1}.", myObject,
System.Convert.ToString(~myIndex));
}
else
{
Console.WriteLine("The object to search for ({0}) is at index
{1}.", myObject, System.Convert.ToString(myIndex));
}
} //FindMyObject
public SamplesArrayList()
{
// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
for (int i = 0; i <= 4; i++)
{
myAL.Add(new System.String(""+(i * 2)));
}
// Locates a specific object that does not exist in the ArrayList.
Object myObjectOdd = new System.String(""+3);
FindMyObject(myAL, myObjectOdd);
}
public static void main(String[] args)
{
new SamplesArrayList();
} //Main
} //SamplesArrayList
Lars-Inge T?nnessen
www.larsinge.com