473,379 Members | 1,330 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

ArrayList Question

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);
}
}
Nov 15 '05 #1
3 1873
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.

"george r smith" <gs****@budgetext.com> wrote in message
news:OX**************@tk2msftngp13.phx.gbl...
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);
}
}

Nov 15 '05 #2
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" <gs****@budgetext.com> wrote in message
news:OX**************@tk2msftngp13.phx.gbl...
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);
}
}

Nov 15 '05 #3
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" <an*******@discussions.microsoft.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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" <gs****@budgetext.com> wrote in message
news:OX**************@tk2msftngp13.phx.gbl...
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);
}
}


Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Alex Ting | last post by:
Hi Everybody, I have an issue about deleting an object from an arrayList. I've bounded a datagrid using this code where it will first run through all of the code in loadQuestions() and bind a...
10
by: C Downey | last post by:
Hello: I have an arraylist storing some very basic objects. The object is very basic, it has 2 properties : ID, and COUNT Before I add an object to the arraylist, I want to check if an...
6
by: GrandpaB | last post by:
While writing this plea for help, I think I solved my dilemma, but I don't know why the problem solving statement is necessary. The inspiration for the statement came from an undocumented VB...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
3
by: Mark Jones | last post by:
I am quite new to ASP and .Net development and I am building a web-based multiple choice exam application. The web page displays the questions using a Repeater control and the answers are nested...
11
by: Simon Says | last post by:
Hi all, I've an arraylist A1 that contains {0,1,2,3,4,5,9,8,7} I've another arraylist A2 that contains {4,5,9} I would like to search whether if A2 is present in A1 and maybe return the first...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
3
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: ...
1
by: =?Utf-8?B?SkI=?= | last post by:
Hello My pgm1 (User Interface Level) passes an empty ArrayList to pgm2 (Business Logic Level). pgm2 then calls pgm3 (Data Access Level) to populate the ArrayList. Question1: When pgm2 gets...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.