Holy crap! "foreach" isn't indexed!

  • Thread starter Thread starter Jon Davis
  • Start date Start date
J

Jon Davis

Wow. I just wasted another two weeks because I didn't know that "foreach"
and the ArrayList class isn't in indexed order!!

a;lsdkjf ;alsdkfj ;alsdkfj ;alsdkf j;adlskfj ;lsadkjf ;lsadkjf ;laskdfj

God bless you! a;lsdkjf;las djkf


Sincerely,

Jon Davis
http://www.powerblog.net/
 
Umm im not sure what you are talking about this works perfectly.

class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ArrayList list = new ArrayList(50);
for( int i = 0; i < 50; i++)
{
list.Add(i);
}
foreach(int i in list)
{
Console.WriteLine("" + i);
}
Console.ReadLine();
}

}

it adds them in order and returns them in order.
 
Yes, that works, but try doing a bunch of inserts and other reordering
operations.

Jon


Dave Quigley said:
Umm im not sure what you are talking about this works perfectly.

class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ArrayList list = new ArrayList(50);
for( int i = 0; i < 50; i++)
{
list.Add(i);
}
foreach(int i in list)
{
Console.WriteLine("" + i);
}
Console.ReadLine();
}

}

it adds them in order and returns them in order.

Jon Davis said:
Wow. I just wasted another two weeks because I didn't know that "foreach"
and the ArrayList class isn't in indexed order!!

a;lsdkjf ;alsdkfj ;alsdkfj ;alsdkf j;adlskfj ;lsadkjf ;lsadkjf ;laskdfj

God bless you! a;lsdkjf;las djkf


Sincerely,

Jon Davis
http://www.powerblog.net/
 
Jon Davis said:
Yes, that works, but try doing a bunch of inserts and other reordering
operations.

As I said before, could you give an example?

Here's an example which does two inserts and a remove - still looks
fine to me:

using System.Collections;
using System;

class Test
{

static void Main(string[] args)
{
ArrayList list = new ArrayList(50);
for( int i = 0; i < 50; i++)
{
list.Add(i);
}
list.Insert (21, "After twenty");
list.Insert (11, "After ten");
list.Remove (30);

foreach(object o in list)
{
Console.WriteLine(o);
}
Console.ReadLine();
}
}
 
Hi Jon,

Consider an ArrayList containing the strings "A" to "J".

If I delete "E" from the list and then add "E" to the list, the
new "E" will be added to the end of the list. That is how Add() is
defined.

The resulting list will be "A","B","C","D","F","G","H","I","J","E"

Is this what you mean by not being in order?

If so, you're right. But it's how it's <supposed> to work.

Fortunately, it's quite feasible to sort the array when you need
it in order.

Regards,
Fergus
 
Fergus Cooney said:
Hi Jon,

Consider an ArrayList containing the strings "A" to "J".

If I delete "E" from the list and then add "E" to the list, the
new "E" will be added to the end of the list. That is how Add() is
defined.

The resulting list will be "A","B","C","D","F","G","H","I","J","E"

Is this what you mean by not being in order?

No. I was getting what appeared to be random order when I used foreach, but
when I used "for (int i=0; ...) { obj = list ..}" everything was in
perfect order. I will try to demonstrate when I can replicate the problem...

... in the mean time, nevermind; I haven't been able to replicate the problem
in an empty project yet.

Jon
 
Back
Top