假设以下是我们的数组-
String[] str = { "P", "Q", "R", "S", "T", "U", "V", "W" };
现在将上述数组的元素设置为新的List-
int len = str.length; List<String>list1 = new ArrayList<String>(); for (int i = 0; i < len; i++) list1.add(str[i]);
考虑一个没有元素的新列表-
List<String>list2 = new ArrayList<String>(); for (int i = 0; i < len; i++) list2.add("");
现在将值从一个列表复制到另一个列表-
Collections.copy(list2,list1);
import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.ListIterator; public class Demo { public static void main(String[] args) { String[] str = { "P", "Q", "R", "S", "T", "U", "V", "W" }; int len = str.length; List<String>list1 = new ArrayList<String>(); for (int i = 0; i < len; i++) list1.add(str[i]); List<String>list2 = new ArrayList<String>(); for (int i = 0; i < len; i++) list2.add(""); Collections.copy(list2,list1); ListIterator<String>iterator = list2.listIterator(); System.out.println("New List..."); while (iterator.hasNext()) System.out.println(iterator.next()); } }
输出结果
New List... P Q R S T U V W