package test;


import java.util.Arrays;
import java.util.List;


/**
* Created by Administrator on 2016/3/6.
*/
public class Test {
public static void main(String[] args) {
String[]arr={"a","b","c"};
List<String> list = Arrays.asList(arr);//数组转换为集合
for (String s : list) {
System.out.println(s);
}


String[] array = list.toArray(new String[list.size()]);//集合转换为数组特别注意int类型的数组转换
for (String str : array) {
System.out.println(str);
}


Object[] objects = list.toArray();
for (Object object : objects) {
System.out.println("object:"+object);
}


int[] nums={1,2,3};
List<int[]> numList = Arrays.asList(nums);//将整个数组作为List的一个元素
System.out.println(numList.size());//size=1
System.out.println();
int[] nums1 = numList.get(0);//获取数组


Integer [] num2={4,5,6};
List<Integer> numList2 = Arrays.asList(num2);//用对应的包装类进行转换
System.out.println(numList2.size());//size=3
}


}

 

这里的toArray()是被重载的没有具体的参数转换的为Object的类型的数组。

加上参数转换为指定类型的数组。

java 中集合和数组互相转换的更多相关文章

  1. JAVA中集合转数组遍历

    JAVA中集合的遍历的一种方法时集合转数组遍历,也是就调用Collection中的toArray(). 代码: public static void main(String[] args) {     ...

  2. Java中List与数组互相转换

    1.说明 在Java中,经常遇到需要List与数组互相转换的场景. List转换成数组,可以使用List的toArray()或者toArray(T[] a)方法. 数组转换成List,可以使用Arra ...

  3. java中List与数组的转换

    1.数组转换成List public static <T> List<T> asList(T... a) String[] arr = new String[] {" ...

  4. java中集合,数组,字符串相互转换

    数组转List String[] staffs = new String[]{"Tom", "Bob", "Jane"}; List sta ...

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

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

  6. java中集合Collection转list对象

    参考:java中集合Collection转list对象 首先我的需求是获取到购物车列表,购物车列表是一个Map对象,构造方法获取购物项,这里购物项是Collection对象 // 购物项集合,K商品I ...

  7. Java中集合的概述

    一.集合和数组的区别 1.数组(可以存储基本数据类型)是用来存现对象的一种容器,但是数组的长度固定,不适合在对象数量未知的情况下使用. 2.集合(只能存储对象,对象类型可以不一样)的长度可变,可在多数 ...

  8. Java中数据类型及其之间的转换

    Java中数据类型及其之间的转换 基本的数据类型 基本类型有以下四种:1)int长度数据类型有:byte(8bits).short(16bits).int(32bits).long(64bits).2 ...

  9. 在Java中怎样把数组转换为ArrayList?

    翻译自:How to Convert Array to ArrayList in Java? 本文分析了Stack Overflow上最热门的的一个问题的答案,提问者获得了很多声望点,使得他得到了在S ...

随机推荐

  1. 用 Freemarker 生成 word 文档(包含图片)

    1. 用word写一个需要导出的word模板,然后存为xml格式. 2. 将xml中需要动态修改内容的地方,换成freemarker的标识符,例如: <w:p wsp:rsidR="0 ...

  2. 静态页分页功能js代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. tableviewCell折叠状态2

    // //  LHQContentViewCell.h //  11 - 投资管理 - 李洪强 // //  Created by vic fan on 16/4/12. //  Copyright ...

  4. Medical Image Processing Conference and Journal 医学图像处理会议与期刊

    会议: Information Processing in Medical Imaging,IPMI IPMI2013 International Conference on Medical Imag ...

  5. 源码安装Postgresql9.4.1

    1.先到官网下载http://www.postgresql.org/ftp/source/v9.4.1/ tar包 2.解压后执行: sudo apt-get install zlib1g-dev s ...

  6. Python 安装mssql (Ubuntu)

    1. Python.h:没有那个文件或目录 apt-get install python-dev 2.sqlfront.h:没有那个文件或目录 apt-get install freetds-dev

  7. C# - Dbhelp

    dbhelp.cs using System; using System.Data; using System.Data.Common; using System.Configuration; pub ...

  8. lucene 3.0.2 搜索

    1.lucene 词频 转载:http://mxdxm.iteye.com/blog/989031 lucene in action作为action系列,确实坚持了其实用性的特色.全书花了很大的篇幅来 ...

  9. Eclipse代码提示功能是不是利用反射

    希望看到这个空文章的你能够说说你的意见………………

  10. RAID 容量计算器

    https://www.synology.com/zh-cn/support/RAID_calculator   磁盘阵列比较表   n/2 n/2 n n/2 安全性高 综合RAID 0/1优点,理 ...