Strange ArrayList behavior?
-
I am having some very strange problems with ArrayList in a class I've written. When I create and populate an ArrayList, only the odd indexes are populated, and the evens are no where to be found. When I try to iterate through the list, it fails on the first index with an "Object not set to an instance" error (since index 0 is technically even). If I start on index 1, it succeeds, but fails when it gets to index 2. When I watch the ArrayList in VS.NET's debugger, I get a list like this: list - {Count(5)} [1] - object 0 [3] - object 1 [5] - object 2 [7] - object 3 [9] - object 4 When it should be this: list - {Count(5)} [0] - object 0 [1] - object 1 [2] - object 2 [3] - object 3 [4] - object 4 Has anyone ever encountered something like this before? Any ideas what could possibly be causing such odd behavior with the ArrayList object? Thanks for any help.
-
I am having some very strange problems with ArrayList in a class I've written. When I create and populate an ArrayList, only the odd indexes are populated, and the evens are no where to be found. When I try to iterate through the list, it fails on the first index with an "Object not set to an instance" error (since index 0 is technically even). If I start on index 1, it succeeds, but fails when it gets to index 2. When I watch the ArrayList in VS.NET's debugger, I get a list like this: list - {Count(5)} [1] - object 0 [3] - object 1 [5] - object 2 [7] - object 3 [9] - object 4 When it should be this: list - {Count(5)} [0] - object 0 [1] - object 1 [2] - object 2 [3] - object 3 [4] - object 4 Has anyone ever encountered something like this before? Any ideas what could possibly be causing such odd behavior with the ArrayList object? Thanks for any help.
How are you populating the ArrayList? Using the constructor that takes an ICollection as parameter? Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
How are you populating the ArrayList? Using the constructor that takes an ICollection as parameter? Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
ArrayList list = new ArrayList(); foreach (obj in objectarray) { list.Add(obj); } return list; Thats basically it. I'm not doing anything special, or out of the ordinary. I'm just using the arraylist at its most basic level.
Very strange. What exception do you get when you try to access index 0? Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
ArrayList list = new ArrayList(); foreach (obj in objectarray) { list.Add(obj); } return list; Thats basically it. I'm not doing anything special, or out of the ordinary. I'm just using the arraylist at its most basic level.
From the c# programmer's reference:
The foreach statement repeats a group of embedded statements for each
element in an array or an object collection. The foreach statement is used to
iterate through the collection to get the desired information, but should not
be used to change the contents of the collection to avoid unpredictable side
effects. The statement takes the following form.....If you change the collection, the iterator is no longer valid. [the first time I posted this, I accidentally replied to S. Senthil Kumar. sorry for the mistake] [P.S. I actually did reply to Jon Rista this time, but it still shows as a reply to S. Senthil Kumar. I don't know what I can do.]
-
From the c# programmer's reference:
The foreach statement repeats a group of embedded statements for each
element in an array or an object collection. The foreach statement is used to
iterate through the collection to get the desired information, but should not
be used to change the contents of the collection to avoid unpredictable side
effects. The statement takes the following form.....If you change the collection, the iterator is no longer valid. [the first time I posted this, I accidentally replied to S. Senthil Kumar. sorry for the mistake] [P.S. I actually did reply to Jon Rista this time, but it still shows as a reply to S. Senthil Kumar. I don't know what I can do.]
Well that's true, but he is iterating over an object array and is changing the newly created ArrayList so this shouldn't be the problem...
-
ArrayList list = new ArrayList(); foreach (obj in objectarray) { list.Add(obj); } return list; Thats basically it. I'm not doing anything special, or out of the ordinary. I'm just using the arraylist at its most basic level.
Is it possible the the objectarray contains null values? Try this:
ArrayList list = new ArrayList();
foreach (obj in objectarray)
{
if (obj != null)
list.Add(obj);
} -
Well that's true, but he is iterating over an object array and is changing the newly created ArrayList so this shouldn't be the problem...
Sorry, I must have read what I thought he was saying rather than than the actual code. I'll have to get these glasses checked!:)
-
Is it possible the the objectarray contains null values? Try this:
ArrayList list = new ArrayList();
foreach (obj in objectarray)
{
if (obj != null)
list.Add(obj);
}Even in that case, he should be getting null values when he indexes into the array, not exceptions. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Even in that case, he should be getting null values when he indexes into the array, not exceptions. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
That's right, but I assume he didn't just iterate but also tried to call some method on those elements.
-
That's right, but I assume he didn't just iterate but also tried to call some method on those elements.
There were nulls in the arraylist. I was populating it by traversing a RedBlack Tree (similar to a B-Tree), and I forgot to check for the nil nodes. Once I added the check for nil nodes in, the problem was solved. I guess that if you add nulls to an ArrayList, adding a watch on that ArrayList will only show the non-null components. The thing that I still don't understand is why I keept getting an exception when I tried to retrieve the elements. Even if the element was null at a given index, I should get a null result, not an exception. Regardless, the problem is solved now. Thanks for all the replies. :)