将 Java LinkedHashSet 的所有元素复制到一个对象数组
首先,创建一个 LinkedHashSet 并添加元素 −
LinkedHashSet<String> l = new LinkedHashSet<String>();
l.add(new String("1"));
l.add(new String("2"));
l.add(new String("3"));
l.add(new String("4"));
l.add(new String("5"));
l.add(new String("6"));
l.add(new String("7"));现在,通过以下方式将其复制到一个对象数组 −
// copying Object[] arr = l.toArray();
下面是一个将 LinkedHashSet 的所有元素复制到一个对象数组的示例 −
示例
import java.util.*;
public class Demo {
public static void main(String[] args) {
LinkedHashSet<String> l = new LinkedHashSet<String>();
l.add(new String("1"));
l.add(new String("2"));
l.add(new String("3"));
l.add(new String("4"));
l.add(new String("5"));
l.add(new String("6"));
l.add(new String("7"));
System.out.println("LinkedHashSet elements...");
System.out.println(l);
// copying
Object[] arr = l.toArray();
System.out.println("Object Array: ");
for (Object res: arr){
System.out.println(res);
}
}
}输出
LinkedHashSet elements... [1, 2, 3, 4, 5, 6, 7] Object Array: 1 2 3 4 5 6 7
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP