|
-
Jun 3rd, 2006, 01:02 PM
#1
Thread Starter
Hyperactive Member
[1.0/1.1] Copying an ArrayList to another
Hi. I'm passing an arraylist to a method in which I want to copy it. I did it as:
ArrayList al....
add values to al....
call method: method(al)
...
another class:
ArrayList te;
.....
public void method(ArrayList al)
{ te = al;
}
Now it is assigning, but if I'm back in the original class and I change ArrayList al, the value of te is also changing. Does anyone knows how to fix this problem? I just want to copy the value of al to te in the method, not the actual pointer to the ArrayList.
-
Jun 3rd, 2006, 01:46 PM
#2
Thread Starter
Hyperactive Member
Re: [1.0/1.1] Copying an ArrayList to another
This is working:
for(int x=0 ; x<al.Count ; x++)
{
this.te.Add(al[x].ToString());
}
But is there a shorter way of doing this?
-
Jun 3rd, 2006, 03:58 PM
#3
Re: [1.0/1.1] Copying an ArrayList to another
use:
Code:
mySourceArray.CopyTo(myTargetArray, Index);
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jun 3rd, 2006, 04:04 PM
#4
Thread Starter
Hyperactive Member
Re: [1.0/1.1] Copying an ArrayList to another
 Originally Posted by ComputerJy
use:
Code:
mySourceArray.CopyTo(myTargetArray, Index);
Its:
Array.Copy(source, 0, Dest, 0, source.count);
But you have to convert the arrayLists as Arrays. I don't want to convert the ArrayLists to arrays, I want when it is finished copying, the dest and source are still both arraylists.
-
Jun 3rd, 2006, 04:10 PM
#5
Re: [1.0/1.1] Copying an ArrayList to another
No, I'm talking about srcArray.CopyTo()
it's not a static method, so you can call it from an instance of the ArrayList
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jun 3rd, 2006, 04:31 PM
#6
Thread Starter
Hyperactive Member
Re: [1.0/1.1] Copying an ArrayList to another
I'm getting this error:
Argument '2': cannot convert from 'System.Collections.ArrayList' to 'System.Array'
from this code:
Main.CopyTo(0, CopyValue, 0, Main.Count);
where main is the source arraylist and CopyValue is the destination arraylist.
I also tried
Main.CopyTo(CopyValue, 0);
But I was getting the following error
The best overloaded method match for 'System.Collections.ArrayList.CopyTo(System.Array, int)' has some invalid arguments
-
Jun 3rd, 2006, 04:35 PM
#7
Re: [1.0/1.1] Copying an ArrayList to another
Sorry, it seems that CopyTo only copies into an array.
use:
Code:
ArrayList b=arrayListA.Clone();
This is supposed to work (hopefully)
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jun 3rd, 2006, 04:37 PM
#8
Re: [1.0/1.1] Copying an ArrayList to another
or you can do this:
Code:
arrayListB = arrayListA.GetRange(0, arrayListA.Count);
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jun 14th, 2006, 12:16 PM
#9
Thread Starter
Hyperactive Member
Re: [1.0/1.1] Copying an ArrayList to another
I got figured it out:
ArrayList al....
add values to al....
call method: method(al)
...
another class:
ArrayList te;
.....
public void method(ArrayList al)
{ // te = al; -> this is wrong since it would copy the actual pointer to al, so
// if I change te, al will also be changed.
te = new ArrayList(al); // This is copy by value only not pointers!
}
Thanks computer jr
-
Jun 14th, 2006, 06:35 PM
#10
Re: [1.0/1.1] Copying an ArrayList to another
You should use the Clone method as cj suggested in post #7. Clone is the standard way to create a shallow copy of an object, assuming that the type implements IClonable, which Clone is a member of.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|