假设以下是带有值的向量-
Vector<String> v = new Vector<String>(); v.add("20"); v.add("40"); v.add("60"); v.add("80"); v.add("100");
现在,将上面的Vector转换为List-
List<String>myList = new ArrayList<String>(v);
以下是在Java中将Vector转换为List的程序-
import java.util.*; public class Demo { public static void main(String[] args) { Vector<String> v = new Vector<String>(); v.add("20"); v.add("40"); v.add("60"); v.add("80"); v.add("100"); v.add("120"); v.add("140"); v.add("160"); v.add("200"); System.out.println("Vector elements = " + v); List<String>myList = new ArrayList<String>(v); System.out.println("List (Vector to List) = " + myList); } }
输出结果
Vector elements = [20, 40, 60, 80, 100, 120, 140, 160, 200] List (Vector to List) = [20, 40, 60, 80, 100, 120, 140, 160, 200]
让我们看另一个例子-
import java.util.*; public class Demo { public static void main(String[] args) { Vector<String> v = new Vector<String>(); v.add("20"); v.add("40"); v.add("60"); v.add("80"); v.add("100"); v.add("120"); v.add("140"); v.add("160"); v.add("200"); System.out.println("Vector elements = " + v); List<String>myList = Collections.list(v.elements()); System.out.println("List (Vector to List) = " + myList); } }
输出结果
Vector elements = [20, 40, 60, 80, 100, 120, 140, 160, 200] List (Vector to List) = [20, 40, 60, 80, 100, 120, 140, 160, 200]