Logo

Programming-Idioms

  • Fortran
  • Java

Idiom #134 Create a new list

Declare and initialize a new list items, containing 3 elements a, b, c.

import java.util.List;
import java.util.ArrayList;
List<T> items = new ArrayList<>();
items.add(a);
items.add(b);
items.add(c);

T is the type of a, b, c.
import java.util.List;
import java.util.Arrays;
List<T> items = Arrays.asList(a, b, c);

T is the type of a, b, c.
import static java.util.List.of;
import java.util.ArrayList;
import java.util.List;
List<T> items = new ArrayList<>(of(a, b, c));

Mutable
import java.util.List;
var items = List.of(a, b, c);

Since Java 9
integer, dimension(3) :: items
items = [a,b,c]
(def items (list a b c))

New implementation...