473,401 Members | 2,127 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,401 software developers and data experts.

Best way to clone an ArrayList?

I was just looking for some opinions on the best way to clone an
ArrayList object. For example, if class A contains an ArrayList, what
would be the best way to make a copy of it for class B (but not using
the class constructors):

class A
{
public A()
{
// constructor stuff goes here
}

public void DoStuff()
{
ArrayList arrTPB = new ArrayList();

arrTPB.Add("Ricky");
arrTPB.Add("Julian");
arrTPB.Add("Bubbles");

// new B object
B myB = new B();

// copy arrTPB from class A to arrNames of myB
// ???
}
}
class B
{
public ArrayList arrNames;

public B()
{
arrNames = new ArrayList();
// other constructor stuff goes here
}
}

Thanks in advance.

Chris
Nov 17 '05 #1
6 5055
hi,

i'm not sure if i understood the question properly but couldnt you just copy
and paste the class?

--
Alvo von Cossel I of Germany
"Chris Bellini" wrote:
I was just looking for some opinions on the best way to clone an
ArrayList object. For example, if class A contains an ArrayList, what
would be the best way to make a copy of it for class B (but not using
the class constructors):

class A
{
public A()
{
// constructor stuff goes here
}

public void DoStuff()
{
ArrayList arrTPB = new ArrayList();

arrTPB.Add("Ricky");
arrTPB.Add("Julian");
arrTPB.Add("Bubbles");

// new B object
B myB = new B();

// copy arrTPB from class A to arrNames of myB
// ???
}
}
class B
{
public ArrayList arrNames;

public B()
{
arrNames = new ArrayList();
// other constructor stuff goes here
}
}

Thanks in advance.

Chris

Nov 17 '05 #2
Reply in text:

"Chris Bellini" <ch*************@spam.chrisbellini.blows.com> wrote in
message news:et**************@TK2MSFTNGP09.phx.gbl...
I was just looking for some opinions on the best way to clone an ArrayList
object. For example, if class A contains an ArrayList, what would be the
best way to make a copy of it for class B (but not using the class
constructors):

class A
{
public A()
{
// constructor stuff goes here
}

public void DoStuff()
{
ArrayList arrTPB = new ArrayList();

arrTPB.Add("Ricky");
arrTPB.Add("Julian");
arrTPB.Add("Bubbles");

// new B object
B myB = new B();

// copy arrTPB from class A to arrNames of myB
// ???
myB.PutNames(arrTPB); }
}
class B
{
public ArrayList arrNames;

public B()
{
arrNames = new ArrayList();
// other constructor stuff goes here
}
public PutNames(ArrayList al)
{
foreach (object obj in ArrayList)
{
if (!obj is string)
continue;
arrNames.Add(obj);
}
} }

Thanks in advance.

Chris

Nov 17 '05 #3
myB.ArrayProperty = arrTPB;

Wouldn't this do what you need?

Chris Bellini wrote:
I was just looking for some opinions on the best way to clone an
ArrayList object. For example, if class A contains an ArrayList, what
would be the best way to make a copy of it for class B (but not using
the class constructors):

class A
{
public A()
{
// constructor stuff goes here
}

public void DoStuff()
{
ArrayList arrTPB = new ArrayList();

arrTPB.Add("Ricky");
arrTPB.Add("Julian");
arrTPB.Add("Bubbles");

// new B object
B myB = new B();

// copy arrTPB from class A to arrNames of myB
// ???
}
}
class B
{
public ArrayList arrNames;

public B()
{
arrNames = new ArrayList();
// other constructor stuff goes here
}
}

Thanks in advance.

Chris


Nov 17 '05 #4

Obvious, yet slick. Thx :)
Chris

Howard Swope wrote:
Reply in text:

"Chris Bellini" <ch*************@spam.chrisbellini.blows.com> wrote in
message news:et**************@TK2MSFTNGP09.phx.gbl...
I was just looking for some opinions on the best way to clone an ArrayList
object. For example, if class A contains an ArrayList, what would be the
best way to make a copy of it for class B (but not using the class
constructors):

class A
{
public A()
{
// constructor stuff goes here
}

public void DoStuff()
{
ArrayList arrTPB = new ArrayList();

arrTPB.Add("Ricky");
arrTPB.Add("Julian");
arrTPB.Add("Bubbles");

// new B object
B myB = new B();

// copy arrTPB from class A to arrNames of myB
// ???

myB.PutNames(arrTPB);
}
}
class B
{
public ArrayList arrNames;

public B()
{
arrNames = new ArrayList();
// other constructor stuff goes here
}

public PutNames(ArrayList al)
{
foreach (object obj in ArrayList)
{
if (!obj is string)
continue;
arrNames.Add(obj);
}
}
}

Thanks in advance.

Chris


Nov 17 '05 #5
You might buy a little performance by using the AddRange method. I think it
still iterates over the collection, but the allocation would be done all at
once.

"Chris Bellini" <ch*************@spam.chrisbellini.blows.com> wrote in
message news:OT**************@TK2MSFTNGP09.phx.gbl...

Obvious, yet slick. Thx :)
Chris

Howard Swope wrote:
Reply in text:

"Chris Bellini" <ch*************@spam.chrisbellini.blows.com> wrote in
message news:et**************@TK2MSFTNGP09.phx.gbl...
I was just looking for some opinions on the best way to clone an
ArrayList object. For example, if class A contains an ArrayList, what
would be the best way to make a copy of it for class B (but not using the
class constructors):

class A
{
public A()
{
// constructor stuff goes here
}

public void DoStuff()
{
ArrayList arrTPB = new ArrayList();

arrTPB.Add("Ricky");
arrTPB.Add("Julian");
arrTPB.Add("Bubbles");

// new B object
B myB = new B();

// copy arrTPB from class A to arrNames of myB
// ???

myB.PutNames(arrTPB);
}
}
class B
{
public ArrayList arrNames;

public B()
{
arrNames = new ArrayList();
// other constructor stuff goes here
}

public PutNames(ArrayList al)
{
foreach (object obj in ArrayList)
{
if (!obj is string)
continue;
arrNames.Add(obj);
}
}
}

Thanks in advance.

Chris



Nov 17 '05 #6
ArrayList.Clone();
"Chris Bellini" <ch*************@spam.chrisbellini.blows.com> schrieb im
Newsbeitrag news:et**************@TK2MSFTNGP09.phx.gbl...
I was just looking for some opinions on the best way to clone an
ArrayList object. For example, if class A contains an ArrayList, what
would be the best way to make a copy of it for class B (but not using
the class constructors):

class A
{
public A()
{
// constructor stuff goes here
}

public void DoStuff()
{
ArrayList arrTPB = new ArrayList();

arrTPB.Add("Ricky");
arrTPB.Add("Julian");
arrTPB.Add("Bubbles");

// new B object
B myB = new B();

// copy arrTPB from class A to arrNames of myB
// ???
}
}
class B
{
public ArrayList arrNames;

public B()
{
arrNames = new ArrayList();
// other constructor stuff goes here
}
}

Thanks in advance.

Chris

Nov 17 '05 #7

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

Similar topics

0
by: Jason Evans | last post by:
Hi All, I am writing my own implementation of queue via a linked list, note not a LinkedList, and was running into trouble with the clone method. I was wondering if anyone could point out some...
0
by: Sasha | last post by:
Hi everybody, I would like to hear your thoughts on the following problem. We have the following classes. Class Exam int ID* int Version* string Name
3
by: Sasha | last post by:
Hi everybody, I would like to hear your thoughts on the following problem. We have the following classes. Class Exam int ID* int Version* string Name
1
by: Hasani | last post by:
I have an object called FileNode and it implements ICloneable which creates a deep copy of the FileNode. The one of the constructors for ArrayList use an ICollection as a paremeter. If I pass that...
1
by: Semut | last post by:
Hello, When do I need to use the Clone function? I am aware that instances that inherited Forms.Control will somehow be of reference types instead of value types. I have tested that ArrayList...
1
by: Jeff Haumesser | last post by:
I have a Collection called Grooming. Each object in this collection contains the property GroomingServices of type Collection. I also have a Clone of this Groomings collection called GroomingsInit....
2
by: bonk | last post by:
I have come across the need to distinguish between the creation of a deep and a shallow copy and with great interest I have read this article: ...
12
by: Jeff | last post by:
I need to read about 100 lines of a csv file into memory. Each line contains 6 fields. What is the best way to store this into memory and then read it back. Is a array of a multi- dimsion...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.