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. C#
  4. ArrayList Indexing: Help

ArrayList Indexing: Help

Scheduled Pinned Locked Moved C#
databasedata-structureshelpquestion
9 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hi, I have an array list with elements. I am unable to access them through indexing. Here ia my code: int index = itemList.Count; Where "itemList" is my "ArrayList". for (int i=0; i < index; i++) { string local = (string)itemList[i]; Console.WriteLine("Printing Array: {0} and I:{1}",(string)itemList[i], i); } My "local" string first gets assigned zero th element of "ArrayList", but when i=1,2,3,4 etc. my "local" string still has zero th element only. I cannot use indexing this way. I have to use "MoveNext()" of Enumerator class to access? Thanks & Regards, Raj

    C J J 4 Replies Last reply
    0
    • L Lost User

      Hi, I have an array list with elements. I am unable to access them through indexing. Here ia my code: int index = itemList.Count; Where "itemList" is my "ArrayList". for (int i=0; i < index; i++) { string local = (string)itemList[i]; Console.WriteLine("Printing Array: {0} and I:{1}",(string)itemList[i], i); } My "local" string first gets assigned zero th element of "ArrayList", but when i=1,2,3,4 etc. my "local" string still has zero th element only. I cannot use indexing this way. I have to use "MoveNext()" of Enumerator class to access? Thanks & Regards, Raj

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Your code is not very readable - the < symbols have been interpreted as the start of HTML tags. Please replace the < symbols with < so that when it is posted it will display correctly. Thanks.


      "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar Coming soon: The Second EuroCPian Event

      L 1 Reply Last reply
      0
      • L Lost User

        Hi, I have an array list with elements. I am unable to access them through indexing. Here ia my code: int index = itemList.Count; Where "itemList" is my "ArrayList". for (int i=0; i < index; i++) { string local = (string)itemList[i]; Console.WriteLine("Printing Array: {0} and I:{1}",(string)itemList[i], i); } My "local" string first gets assigned zero th element of "ArrayList", but when i=1,2,3,4 etc. my "local" string still has zero th element only. I cannot use indexing this way. I have to use "MoveNext()" of Enumerator class to access? Thanks & Regards, Raj

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        Out of curiousity. If all your elements are strings, why not use the System.Collections.Specialized.StringCollection class instead?


        "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar Coming soon: The Second EuroCPian Event

        1 Reply Last reply
        0
        • L Lost User

          Hi, I have an array list with elements. I am unable to access them through indexing. Here ia my code: int index = itemList.Count; Where "itemList" is my "ArrayList". for (int i=0; i < index; i++) { string local = (string)itemList[i]; Console.WriteLine("Printing Array: {0} and I:{1}",(string)itemList[i], i); } My "local" string first gets assigned zero th element of "ArrayList", but when i=1,2,3,4 etc. my "local" string still has zero th element only. I cannot use indexing this way. I have to use "MoveNext()" of Enumerator class to access? Thanks & Regards, Raj

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

          Hey, try this: ArrayList itemList = new ArrayList(); itemList.Add("string goes here"); //it can be any object type Then, you can access them like this: for (int i=0; i < itemList.Count; i++) { Console.WriteLine("Printing Array: {0} and I:{1}", (string)itemList[i], i.ToString() ); }

          U 1 Reply Last reply
          0
          • J Jon G

            Hey, try this: ArrayList itemList = new ArrayList(); itemList.Add("string goes here"); //it can be any object type Then, you can access them like this: for (int i=0; i < itemList.Count; i++) { Console.WriteLine("Printing Array: {0} and I:{1}", (string)itemList[i], i.ToString() ); }

            U Offline
            U Offline
            User 956892
            wrote on last edited by
            #5

            I tried that. Only first element (string)itemList[0] is getting printed. Suppose if count is 10, first element is getting printed 10 times. If I use System.Collections.IEnumerator myEnumerator = itemList.GetEnumerator(); while ( myEnumerator.MoveNext() ) { Console.Write( "\n{0}", myEnumerator.Current ); } I am getting correct output. Thanks & Regards, Raj

            U C 2 Replies Last reply
            0
            • C Colin Angus Mackay

              Your code is not very readable - the < symbols have been interpreted as the start of HTML tags. Please replace the < symbols with < so that when it is posted it will display correctly. Thanks.


              "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar Coming soon: The Second EuroCPian Event

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Hi Sorry for messing up in Copy/paste. Here is the code again. int index = itemList.Count; Where "itemList" is my "ArrayList". for (int i=0; i < index; i++) { string local = (string)itemList[i]; Console.WriteLine("Printing Array: {0} and I:{1}",(string)itemList[i], i); } Again my problem is If there are 10 elements I am getting first element printed 10 times. But If I use System.Collections.IEnumerator myEnumerator = myCollection.GetEnumerator(); while ( myEnumerator.MoveNext() ) Console.Write( "\n{0}", myEnumerator.Current ); I am getting the correct output. Thanks & Regards, Raj

              1 Reply Last reply
              0
              • U User 956892

                I tried that. Only first element (string)itemList[0] is getting printed. Suppose if count is 10, first element is getting printed 10 times. If I use System.Collections.IEnumerator myEnumerator = itemList.GetEnumerator(); while ( myEnumerator.MoveNext() ) { Console.Write( "\n{0}", myEnumerator.Current ); } I am getting correct output. Thanks & Regards, Raj

                U Offline
                U Offline
                User 956892
                wrote on last edited by
                #7

                I got it. Thnaks Raj

                1 Reply Last reply
                0
                • U User 956892

                  I tried that. Only first element (string)itemList[0] is getting printed. Suppose if count is 10, first element is getting printed 10 times. If I use System.Collections.IEnumerator myEnumerator = itemList.GetEnumerator(); while ( myEnumerator.MoveNext() ) { Console.Write( "\n{0}", myEnumerator.Current ); } I am getting correct output. Thanks & Regards, Raj

                  C Offline
                  C Offline
                  Colin Angus Mackay
                  wrote on last edited by
                  #8

                  You know that if a object supports the IEnumerable interface (which provides the GetEnumerator() method) that you can use it in a foreach loop? Here is an article about Using IEnumerator and IEnumerable in the .NET Framework[^]


                  "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar Coming soon: The Second EuroCPian Event

                  1 Reply Last reply
                  0
                  • L Lost User

                    Hi, I have an array list with elements. I am unable to access them through indexing. Here ia my code: int index = itemList.Count; Where "itemList" is my "ArrayList". for (int i=0; i < index; i++) { string local = (string)itemList[i]; Console.WriteLine("Printing Array: {0} and I:{1}",(string)itemList[i], i); } My "local" string first gets assigned zero th element of "ArrayList", but when i=1,2,3,4 etc. my "local" string still has zero th element only. I cannot use indexing this way. I have to use "MoveNext()" of Enumerator class to access? Thanks & Regards, Raj

                    J Offline
                    J Offline
                    Jeff Varszegi
                    wrote on last edited by
                    #9

                    I suspect that you're getting confused somewhere along the line. Paste the exact code below into your project, run it, and report back: ArrayList itemList = new ArrayList(); itemList.Add("0"); itemList.Add("1"); itemList.Add("2"); itemList.Add("3"); itemList.Add("4"); itemList.Add("5"); itemList.Add("6"); itemList.Add("7"); itemList.Add("8"); itemList.Add("9"); int count = itemList.Count; for (int i=0; i < count; i++) { Console.WriteLine("Printing Array: {0} and I:{1}", itemList[i], i); } You're probably confused about assigning int variables. I imagine that you take a read of the Count property before you fill the ArrayList, yes? Well, that object is assigned by value, not by reference, because int is a value type. The part where you populate the array is the only piece missing in the information you gave us; notice the ensuing confusion? Next time, I'd include all the code that's probably part of the problem. I'm not being crusty, just trying to give helpful advice. Anyway, notice that above I read Count after filling the data. This is better on performance than reading Count every trip through the loop. ArrayList is built specifically to replicate the semantics of accessing an array by index, and it uses an array internally to hold its data. It's probably silly to use an enumerator in your case. Enumerators were made so that all lists can be considered just to be implementations of IList. Your explicit casts are unnecessary, but I left them in. Regards, Jeff Varszegi

                    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