ArrayList Question

G

george r smith

I am trying to create an arrayList that contains multiple arrayLists.
My code attempt is below. The question I have is how can I get away
from creating another pAttribute list than can be added to pItem.

As I understand it (and I coded it to test) if I try to use pAttribute again
by clearing with pAttribute.Clear because it is a shallow copy the
pItem[0] is also cleared. I guess I need a "deep copy" but can't find
any documentation on it.

So is there any way to pItem[1].Add({100.00,200.00}). This will not compile
but
it shows what I want to do.

I might add that I want to use ArrayList because you are not restricted to
one type
as in arrays.


thanks
grs


static void Main(string[] args)
{
ArrayList pItem = new ArrayList();
ArrayList pAttribute = new ArrayList();
for (int i = 101; i < 105; i++)
{
pAttribute.Add(i);
}
pItem.Add(pAttribute);
PrintValues((ArrayList)pItem[0]);
}

public static void PrintValues(ArrayList myList)
{
IEnumerator myEnumerator = myList.GetEnumerator();
while (myEnumerator.MoveNext())
Console.WriteLine(myEnumerator.Current);
}
}
 
P

Peter Rilling

There are a few ways.

If you want to follow your suggested attempt, you might need to initialize
the array like so -- .Add(new int[]{100.00, 200.00}).

Alternatively, you could like into using the ArrayList.CopyTo(...) method.

Or try the Object.MemberwiseClone(...) since you are really only interested
in preserving pAttribute, this might be sufficient.
 
M

Matt Burland

If you want to use pAttribute again without trashing the ArrayList you've
already created just do:

pAttribute = new ArrayList();

The pAttribute reference will now point to a *different* ArrayList and your
old ArrayList will still be intact and won't be garbage collected because
it's referenced by pItem[0] (note: if you then null pItem then your old
ArrayList will be now eligible for garbage collection).
As you pointed out pAttribute.Clear() will clear pItem[0] because they are
both references to the SAME object, you didn't make a copy at all and you
probably don't want to (whether you know it or not).
As for using ArrayList instead of arrays, you are wrong about arrays. An
object[] array can contain a mix of whatever items you want since all
classes inherit from object. So if you don't need the dynamic resizing
abilities of the ArrayList, you're better off with object[].
 
G

george r smith

Matt,
Thanks for the info on object[] array - now that you have told me I
understand :).
I do need dynamic so I will keep on the ArrayList. Thanks again.
grs

Matt Burland said:
If you want to use pAttribute again without trashing the ArrayList you've
already created just do:

pAttribute = new ArrayList();

The pAttribute reference will now point to a *different* ArrayList and your
old ArrayList will still be intact and won't be garbage collected because
it's referenced by pItem[0] (note: if you then null pItem then your old
ArrayList will be now eligible for garbage collection).
As you pointed out pAttribute.Clear() will clear pItem[0] because they are
both references to the SAME object, you didn't make a copy at all and you
probably don't want to (whether you know it or not).
As for using ArrayList instead of arrays, you are wrong about arrays. An
object[] array can contain a mix of whatever items you want since all
classes inherit from object. So if you don't need the dynamic resizing
abilities of the ArrayList, you're better off with object[].


george r smith said:
I am trying to create an arrayList that contains multiple arrayLists.
My code attempt is below. The question I have is how can I get away
from creating another pAttribute list than can be added to pItem.

As I understand it (and I coded it to test) if I try to use pAttribute again
by clearing with pAttribute.Clear because it is a shallow copy the
pItem[0] is also cleared. I guess I need a "deep copy" but can't find
any documentation on it.

So is there any way to pItem[1].Add({100.00,200.00}). This will not compile
but
it shows what I want to do.

I might add that I want to use ArrayList because you are not restricted to
one type
as in arrays.


thanks
grs


static void Main(string[] args)
{
ArrayList pItem = new ArrayList();
ArrayList pAttribute = new ArrayList();
for (int i = 101; i < 105; i++)
{
pAttribute.Add(i);
}
pItem.Add(pAttribute);
PrintValues((ArrayList)pItem[0]);
}

public static void PrintValues(ArrayList myList)
{
IEnumerator myEnumerator = myList.GetEnumerator();
while (myEnumerator.MoveNext())
Console.WriteLine(myEnumerator.Current);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top