List,Set转换为数组的方法。

toArray函数有两种形式,一种无参数,一种带参数,注意带参数形式中,要指明数组的大小。

public void convertCollectionToArray() {
List list = new ArrayList();
Object[] objectArray1 = list.toArray();
String[] array1 = list.toArray(new String[list.size()]); Set set = new HashSet();
Object[] objectArray2 = set.toArray();
String[] array2 = set.toArray(new String[set.size()]);
}

反过来,数组转换为List,Set。

Integer[] numbers = {, , , , , , , , , , };
// To convert an array into a Set first we convert it to a List. Next
// with the list we create a HashSet and pass the list as the constructor.
List list = Arrays.asList(numbers);
Set set = new HashSet(list);

注意:对于int[]数组不能直接这样做,因为asList()方法的参数必须是对象。应该先把int[]转化为Integer[]。对于其他primitive类型的数组也是如此,必须先转换成相应的wrapper类型数组。

  int[] numbers = {, , , , , , , , , , };
int size = numbers.length;
Integer[] array = new Integer[size];
for (int i = ; i < numbers.length; i++) {
Integer integer = numbers[i];
array[i] = integer;
}
List list = Arrays.asList(array);
 
 

Java:集合与数组转换的更多相关文章

  1. Java集合和数组的区别

    参考:Java集合和数组的区别 集合和容器都是Java中的容器. 区别 数组特点:大小固定,只能存储相同数据类型的数据 集合特点:大小可动态扩展,可以存储各种类型的数据   转换 数组转换为集合: A ...

  2. JAVA 集合List,数组,Set,Map,直接的相互转换

    Java集合转换[List<-->数组.List<-->Set.数组<-->Set.Map-->Set.Map-->List] //List--> ...

  3. java 集合与数组的互转方法,与源码分析

    前言 java数组与集合需要互相转换的场景非常多,但是运用不好还是容易抛出UnSupportedOperationException.下面讲解一下互转的方法,以及结合源码分异常产生的原因 集合转数组 ...

  4. 【Java】字节数组转换工具类

    import org.apache.commons.lang.ArrayUtils; import java.nio.charset.Charset; /** * 字节数组转换工具类 */ publi ...

  5. java集合与数组之间转换

    数组转换为集合 采用java中集合自带的asList()方法就可以完成转换了 String[] array = new String[] {"zhu", "wen&quo ...

  6. Java -- List与数组转换

    list转数组 使用for循环 使用list.toArray(new String[]),不可以强制转换list.toArray(),因为数组在jvm是一个object表示的,是一个对象 数组转lis ...

  7. Java集合和数组的比较(为什么引入集合)

    数组不是面向对象的,存在明显的缺陷,集合完全弥补了数组的一些缺点,比数组更灵活更实用,可大大提高软件的开发效率而且不同的集合框架类可适用于不同场合.具体如下: 1)数组的效率高于集合类. 2)数组能存 ...

  8. Java集合与数组的联系和区别

    数组特点 存放一组相同的数据类型(基本类型和对象类型)的数据,从而实现对数据的管理. 优势:可以快速的通过下标对数组元素进行访问,效率高 劣势:容量实现定义好了,不能随着需求变化而扩容. 集合特点 集 ...

  9. 【由浅入深理解java集合】(一)——集合框架 Collction、Map

    本篇文章主要对java集合的框架进行介绍,使大家对java集合的整体框架有个了解.具体介绍了Collection接口,Map接口以及Collection接口的三个子接口Set,List,Queue. ...

随机推荐

  1. Windows平台JDK安装

    原文链接:http://android.eoe.cn/topic/android_sdk 下载Java的开发包JDK JDK有好几个类型版本,我们只需要选择Java SE类型的版本就行了.进入网页:h ...

  2. vim的Tab键

    vim中默认的tab键大约是6个空格(目测)的宽度.如果想修改为4个空格,用以下命令:    shiftwidth=4    softtabstop-4shiftwidth的含义是:回车后需要缩进时, ...

  3. iOS面试题及答案大总结

    1.写一个NSString类的实现 + (id)initWithCString:(c*****t char *)nullTerminatedCString encoding:(NSStringEnco ...

  4. angular中的jqLite所包含的jquery API

    Angular本身包含了一个叫做jqLite的可兼容性库. 使用过的angular.element()方法就返回一个jqLite对象,  jqLite是jQuery库的子集,它 允许Angular以跨 ...

  5. 在sublime text3中安装sass编译scss文件

    一 搭建环境 首先安装ruby环境,不然会编译失败,在这里下载ruby ,安装的时候选择第二项 在cmd中输入gem -v 显示版本号说明ruby安装成功 待ruby安装成功后,在cmd中输入 gem ...

  6. SeekBar: 修改SeekBar中进度条的高度

    SeekBar中有两个很特别的属性需要留意下: 1.android:maxHeight和android:minHeight .前者是用来指定进度条最大高度的(此高度并非SeekBar整个控件的高度), ...

  7. Adding support for distinct operation for table API on DataStream

    https://github.com/apache/flink/pull/6521/files/66c3bd5d52a5e4af1f83406035b95774e8b6f636#diff-680b30 ...

  8. CNN卷积可视化与反卷积

    1.<Visualizing and Understanding Convolutional Networks> 2.<Adaptive deconvolutional networ ...

  9. ubuntu 查看软件安装目录以及安装版本

    1.aptitude show 软件名 例如:aptitude show kde-runtime ****@ubuntu:~$ aptitude show kde-runtime 软件包: kde-r ...

  10. 【转帖】(一)unity4.6Ugui中文教程文档-------概要

    原帖至上,移步请戳:(一)unity4.6Ugui中文教程文档-------概要 unity4.6中的一个重要的升级就是GUI ,也把它称为UGUI ,废话我不多说,大家可以百度了解一下. 虽然现在处 ...