How to move one Arraylist data to another arraylist. I have tried for many option but the output is in the form of array not arraylist
In order to copy elements of ArrayList to another ArrayList, we use the Collections. copy() method. It is used to copy all elements of a collection into another. where src is the source list object and dest is the destination list object.
The clone() method of the ArrayList class is used to clone an ArrayList to another ArrayList in Java as it returns a shallow copy of its caller ArrayList.
The ArrayList. equals() is the method used for comparing two Array List. It compares the Array lists as, both Array lists should have the same size, and all corresponding pairs of elements in the two Array lists are equal. Parameters: This function has a single parameter which is an object to be compared for equality.
First - unless you are on .NET 1.1, you should a avoid ArrayList
- prefer typed collections such as List<T>
.
When you say "copy" - do you want to replace, append, or create new?
For append (using List<T>
):
List<int> foo = new List<int> { 1, 2, 3, 4, 5 };
List<int> bar = new List<int> { 6, 7, 8, 9, 10 };
foo.AddRange(bar);
To replace, add a foo.Clear();
before the AddRange
. Of course, if you know the second list is long enough, you could loop on the indexer:
for(int i = 0 ; i < bar.Count ; i++) {
foo[i] = bar[i];
}
To create new:
List<int> bar = new List<int>(foo);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With