arrays

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2009
Posts: 55
Reputation: tintincute is an unknown quantity at this point 
Solved Threads: 0
tintincute tintincute is offline Offline
Junior Poster in Training

arrays

 
0
  #1
Apr 7th, 2009
Hi
I have a code here composed of Array. I would like to know what does this mean here: (please see the red text)
What does it mean?

Thanks & regards


ArrayList numbers = new ArrayList();
string yourValue;

Console.WriteLine("Give the value (b to end)");
Console.WriteLine();

do
{
Console.WriteLine("Give the value:");
yourValue = Console.ReadLine();

if (yourValue == "b")
break;
numbers.Add(Convert.ToInt32(yourValue));
}
while (true);
System.Collections.IEnumerator Enumerator = numbers.GetEnumerator();
while (Enumerator.MoveNext())
Console.WriteLine(" " + Enumerator.Current);
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,160
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: arrays

 
0
  #2
Apr 7th, 2009
this will loop through all the values in your array
IEnumerator basically means you can use it to iterate your data

lets say you have 1, 5, 3, 8 in your array

this will print out

1 5 3 8
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 55
Reputation: tintincute is an unknown quantity at this point 
Solved Threads: 0
tintincute tintincute is offline Offline
Junior Poster in Training

Re: arrays

 
0
  #3
Apr 7th, 2009
so in looping all the valus I always need this one:
System.Collections.IEnumerator Enumerator = numbers.GetEnumerator();
while (Enumerator.MoveNext())
Console.WriteLine(" " + Enumerator.Current);

is that right?

thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,160
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: arrays

 
0
  #4
Apr 7th, 2009
you don't always have to use that way, but you can

you could also do something like this

  1. foreach(int num in numbers)
  2. {
  3. Console.WriteLine(" " + num);
  4. }
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 55
Reputation: tintincute is an unknown quantity at this point 
Solved Threads: 0
tintincute tintincute is offline Offline
Junior Poster in Training

Re: arrays

 
0
  #5
Apr 7th, 2009
i tried this but when I write the "b" to end, I got an error. this is what I've done.


  1. while (true);
  2. //this will loop through all the values in your array IEnumerator basically means you can use it to
  3. //iterate your data
  4. foreach (int num in numbers)
  5. {
  6. Console.WriteLine(" " + num);
  7. }
  8.  
  9.  
  10.  
  11. //System.Collections.IEnumerator Enumerator = numbers.GetEnumerator();
  12. //while (Enumerator.MoveNext())
  13. // Console.WriteLine(" " + Enumerator.Current);
  14. }
Last edited by tintincute; Apr 7th, 2009 at 5:11 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,160
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: arrays

 
0
  #6
Apr 7th, 2009
its because you are using an untyped list for one, but you didn't have an else statement

  1. if (yourValue == "b")
  2. {
  3. break;
  4. }
  5. else
  6. {
  7. numbers.Add(Convert.ToInt32(yourValue));
  8. }

also you could use List instead of ArrayList

  1. List<int> numbers = new List<int>();
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 2,414
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 123
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: arrays

 
0
  #7
Apr 9th, 2009
Originally Posted by dickersonka View Post

also you could use List instead of ArrayList

  1. List<int> numbers = new List<int>();

You don't really need the typed List for what's going on, it's just a nice thing to have in terms of type safety...
Last edited by alc6379; Apr 9th, 2009 at 11:08 pm.
Alex Cavnar, aka alc6379
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 55
Reputation: tintincute is an unknown quantity at this point 
Solved Threads: 0
tintincute tintincute is offline Offline
Junior Poster in Training

Re: arrays

 
0
  #8
Apr 11th, 2009
ok i'll try that again and will publish the results.
Thanks;-)
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 55
Reputation: tintincute is an unknown quantity at this point 
Solved Threads: 0
tintincute tintincute is offline Offline
Junior Poster in Training

Re: arrays

 
0
  #9
Apr 13th, 2009
hi
thanks! it works-)
also the List<> i tried and it also works
have a good day;-)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC