Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #227 Copy a list

Create the new list y containing the same elements as the list x.

Subsequent modifications of y must not affect x (except for the contents referenced by the elements themselves if they contain pointers).

import java.util.ArrayList;
ArrayList<String> y = new ArrayList<>(x);
List<T> y = x.ToList();
List<Int32> y = new List<Int32>(x);
y = [...x];
type (foo), allocatable, dimension(:) :: y
y = x
y := make([]T, len(x))
copy(y, x)
import "slices"
y := slices.Clone(x)
let y = x.slice();
uses classes;
y.assign(x);
use Storable qw(dclone);
my @y = @x; # for simple arrays

# for complex arrays with references:
my @y = @{dclone(\@x)};
y = x[:]
y = x.copy()
y = x.dup
let y = x.clone();

New implementation...
< >
programming-idioms.org