Java Collections copy list

Description

Java Collections copy list

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class Main {
  public static void main(String[] args) {
    String[] langList = { "CSS", "HTML", "C++", "Java", "Javascript" };

    List<String> src = new LinkedList<>();
    for (int i = 0; i < langList.length; i++)
      src.add(langList[i]);// www  . j  a va 2 s. c  o m

    List<String> dst = new ArrayList<>();
    for (int i = 0; i < langList.length; i++)
      dst.add("");

    System.out.println(src);
    System.out.println(dst);
    Collections.copy(dst, src);
    System.out.println(src);
    System.out.println(dst);

    ListIterator<String> liter = dst.listIterator();

    while (liter.hasNext())
      System.out.print(liter.next());
  }
}



PreviousNext

Related