all groups > vj# > march 2004 >
You're in the

vj#

group:

System.InvalidOperationException


System.InvalidOperationException Reshma
3/16/2004 8:01:18 PM
vj#:
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
Re: System.InvalidOperationException Lars-Inge Tønnessen
3/17/2004 12:47:07 PM

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

AddThis Social Bookmark Button