Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Strange ArrayList behavior?

Strange ArrayList behavior?

Scheduled Pinned Locked Moved .NET (Core and Framework)
helpcsharpdatabasevisual-studiodebugging
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Jon Rista

    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.

    D Offline
    D Offline
    Dan Neely
    wrote on last edited by
    #2

    Without seeing your source I can't say for certain, but at a guess you're incrementing the index you add at both before and after you insert an element into the array.

    1 Reply Last reply
    0
    • J Jon Rista

      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.

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #3

      How are you populating the ArrayList? Using the constructor that takes an ICollection as parameter? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      J 1 Reply Last reply
      0
      • S S Senthil Kumar

        How are you populating the ArrayList? Using the constructor that takes an ICollection as parameter? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        J Offline
        J Offline
        Jon Rista
        wrote on last edited by
        #4

        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.

        S S R 3 Replies Last reply
        0
        • J Jon Rista

          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.

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #5

          Very strange. What exception do you get when you try to access index 0? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          1 Reply Last reply
          0
          • J Jon Rista

            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.

            S Offline
            S Offline
            Scott Serl
            wrote on last edited by
            #6

            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.]

            R 1 Reply Last reply
            0
            • S Scott Serl

              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.]

              R Offline
              R Offline
              Robert Rohde
              wrote on last edited by
              #7

              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...

              S 1 Reply Last reply
              0
              • J Jon Rista

                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.

                R Offline
                R Offline
                Robert Rohde
                wrote on last edited by
                #8

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

                S 1 Reply Last reply
                0
                • R Robert Rohde

                  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...

                  S Offline
                  S Offline
                  Scott Serl
                  wrote on last edited by
                  #9

                  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!:)

                  1 Reply Last reply
                  0
                  • R Robert Rohde

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

                    S Offline
                    S Offline
                    S Senthil Kumar
                    wrote on last edited by
                    #10

                    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

                    R 1 Reply Last reply
                    0
                    • S S Senthil Kumar

                      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

                      R Offline
                      R Offline
                      Robert Rohde
                      wrote on last edited by
                      #11

                      That's right, but I assume he didn't just iterate but also tried to call some method on those elements.

                      J 1 Reply Last reply
                      0
                      • R Robert Rohde

                        That's right, but I assume he didn't just iterate but also tried to call some method on those elements.

                        J Offline
                        J Offline
                        Jon Rista
                        wrote on last edited by
                        #12

                        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. :)

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups