将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"));现在,将其复制到这样的对象数组中-
//复制 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);
//复制
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
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短