将 Vector 转换为 Java 中的列表的程序
我们假设向量有如下值 −
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]
广告