ArrayList Indexing: Help
-
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
-
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
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
-
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
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
-
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
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() ); }
-
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() ); }
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
-
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
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
-
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
I got it. Thnaks Raj
-
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
You know that if a object supports the
IEnumerable
interface (which provides theGetEnumerator()
method) that you can use it in aforeach
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
-
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
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 theCount
property before you fill theArrayList
, yes? Well, that object is assigned by value, not by reference, becauseint
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 ofIList
. Your explicit casts are unnecessary, but I left them in. Regards, Jeff Varszegi