Merge the contents of one arraylist into another.

P

pmclinn

I have a function that returns an arraylist.

I want the returned arraylist to be copied into the local arraylist.
What is the most efficient way of doing this?



-Peter
 
P

Peter Proost

Hi Peter,

is the array list you return already filled? or is it an empty one, if it's
empty you can do it like this

Private Sub button1_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles button1.Click
Dim arr As New ArrayList
arr = GiveArraylist()
For i As Integer = 0 To arr.Count - 1
MsgBox(arr(i))
Next
End Sub

Private Function GiveArraylist() As ArrayList
Dim arr As New ArrayList
arr.Add("value1")
arr.Add("value2")
arr.Add("value3")
Return arr
End Function

hth Peter
 
H

Herfried K. Wagner [MVP]

pmclinn said:
I want the returned arraylist to be copied into the local arraylist.
What is the most efficient way of doing this?

Assuming 'al1' and 'al2' are variables pointing to different arraylists, and
'al2''s content should be added to 'al1':

\\\
al1.AddRange(al2)
///
 
B

Bob Powell [MVP]

OOPs.. Red herring..Wrong API.

You need to use AddRange.

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs
e)

{

ArrayList a=new ArrayList();

ArrayList b=new ArrayList();

a.Add("One");

a.Add("Two");

a.Add("Three");

a.Add("Four");

a.Add("Five");

b.Add("Six");

b.Add("Seven");

b.Add("Eight");

b.Add("Nine");

b.Add("Ten");

a.AddRange(b);

int y=10;

foreach(string s in a)

{

e.Graphics.DrawString(s,Font,Brushes.Black,10,y);

y+=(int)Font.GetHeight()+4;

}


--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
H

Herfried K. Wagner [MVP]

Bob,

Bob Powell said:
Just use arrayList.Add. There is an overload that takes another arraylist.

Are you sure about this? I don't see this overload for .NET 1.1.
 
C

Cor Ligthert

I want the returned arraylist to be copied into the local arraylist.
Assuming 'al1' and 'al2' are variables pointing to different arraylists,
and 'al2''s content should be added to 'al1':

\\\
al1.AddRange(al2)
///
Are you sure this method is "copying", I have another idea (for copying I
am used to talk about values not references)

Cor
 
H

Herfried K. Wagner [MVP]

Cor,

Cor Ligthert said:
Are you sure this method is "copying", I have another idea (for copying I
am used to talk about values not references)

It will copy the references stored in the arraylist referenced by 'al2' into
the arraylist referenced by 'al1'. So, even references can be "copied", not
only values, which rarely makes sense.

| Subject: Merge the contents of one arraylist into another.

.... which is what my code does.
 

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