1 数组转换为List

调用Arrays类的静态方法asList。

asList

public static <T> List<T> asList(T... a)
Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.) This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray(). The returned list is serializable and implements RandomAccess.

This method also provides a convenient way to create a fixed-size list initialized to contain several elements:

     List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
 
Parameters:
a - the array by which the list will be backed
Returns:
a list view of the specified array

用法:API中提供了一种使用的方法。更为常用的示例代码:

  1. String[] arr = new String[] {"str1", "str2"};
  2. List<String> list = Arrays.asList(arr);

2 List转换为数组

这里的List以ArrayList为例,ArrayList的API提供了两种可供使用的函数。

toArray

public Object[] toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).

The returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.

This method acts as bridge between array-based and collection-based APIs.

Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface List<E>
Overrides:
toArray in class AbstractCollection<E>
Returns:
an array containing all of the elements in this list in proper sequence
See Also:
Arrays.asList(Object[])

toArray

public <T> T[] toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.

If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)

Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface List<E>
Overrides:
toArray in class AbstractCollection<E>
Parameters:
a - the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Returns:
an array containing the elements of the list
Throws:
ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of every element in this list
NullPointerException - if the specified array is null

用法:示例代码:

    1. List<String> list = new ArrayList<String>();
    2. list.add("str1");
    3. list.add("str2");
    4. int size = list.size();
    5. String[] arr = (String[])list.toArray(new String[size]);//使用了第二种接口,返回值和参数均为结果

List与Array之间互换的更多相关文章

  1. JAVA中List与Array之间互换

    1.Array转List ArrayList<String> list = new ArrayList<String>(); String[] arr = new String ...

  2. [Swift]JSON字符串与字典(Dictionary)、数组(Array)之间的相互转换

    1.JSON字符串与字典(Dictionary)之间的相互转换 import Foundation //JSON字符串转换为字典(Dictionary) func getDictionaryFromJ ...

  3. Python PIL 的image类和numpy array之间的互换

    import cv2 import numpy as np from PIL import Image from PIL import ImageEnhance def getline(frame): ...

  4. ArrayList和Array之间的转换

    ArrayList转Array (1):使用ArrayList的toArray方法.     1)当ArrayList中存放的是引用类型时(例如String),成功     /**      * 使用 ...

  5. Java-Collection、Map及Array之间的转换

    1 List -> Map 设个User类: public class User { private String userName; private String userId; privat ...

  6. Java-Collection、Map和Array之间的转换

    1 List -> Map 设个User类: public class User { private String userName; private String userId; privat ...

  7. vector与array之间转换,向量与数据之间转换

    一维数组: vector<int> a; int b[5] = {1,2,3,4,5}; a.push_back(b);   二维数组: b[5][6] = {1,2,3,4,5,6... ...

  8. Vector, ArrayList, Array

    JAVA新手在使用JAVA的时候大概都会遇到这个问题: JAVA中的Array, ArrayList, Vector, List, LinkedList有什么样的区别?尤其是Vector, Array ...

  9. Eigen学习之Array类

    Eigen 不仅提供了Matrix和Vector结构,还提供了Array结构.区别如下,Matrix和Vector就是线性代数中定义的矩阵和向量,所有的数学运算都和数学上一致.但是存在一个问题是数学上 ...

随机推荐

  1. java LinkedList(链表)

    LinkedList也像ArrayList一样实现了基本的List接口,但是它执行某些操作(在List的中间插入和移除)时比ArrayList更高效,但在随机访问方面却要逊色一些 LinkedList ...

  2. v-if案例解析(element-ui form-item 结合 v-if 动态生成rule规则\表单元素,表单无法验证问题剖析 )

    fire 读在最前面: 1.此文章衔接Vue 虚拟Dom 及 部分生命周期初探,相关整体知识点请先阅读后再继续本文阅读 问:当v-if为true时,会重新渲染相关dom节点吗? <child v ...

  3. 2、Centos6 安装tomcat8.5.31

    1.下载 安装包 wget http://mirrors.aliyun.com/apache/tomcat/tomcat-8/v8.5.31/bin/apache-tomcat-8.5.31.tar. ...

  4. 欢迎来到abc2237512422的博客

    这是第一篇博文. 本博客已迁移到 abc233.site

  5. 从 prototype.js 深入学习 javascript 的面向对象特性

    从 prototype.js 深入学习 javascript 的面向对象特性 js是一门很强大的语言,灵活,方便. 目前我接触到的语言当中,从语法角度上讲,只有 Ruby 比它更爽. 不过我接触的动态 ...

  6. Net WebAPI2

    SwaggerUI ASP.Net WebAPI2   目前在用ASP.NET的 WebAPI2来做后台接口开发,在与前台做测试的时候,总是需要发送一个demo给他,但是这样很麻烦的,他还有可能记不住 ...

  7. 使用libvirt管理KVM(一)

    一. 安装和配置libvirt,源码下载http://www.qemu-project.org/download/#source. 二. 从包和源码包进行安装libvirt. 1. 在ubuntu系统 ...

  8. C语言结构体在内存中的存储情况探究------内存对齐

    条件(先看一下各个基本类型都占几个字节): void size_(){ printf("char类型:%d\n", sizeof(char)); printf("int类 ...

  9. CSS3实现原腾讯视频透明边框,多重边框等(关于边框那些不为人知的事情)

    1.hsla或rgba实现半透明边框. rgba在rgb的基础上增加了透明通道,就不详细说了,下面重点说下hsla: 说明: HSLA(H,S,L,A) 取值: H:Hue(色调).0(或360)表示 ...

  10. Nuxt.js 学习笔记

    起源 最主要的原因时使用vue-cli搭建的SPA(单页应用)不利于搜索引擎的SEO操作.搜索引擎对SPA的抓取并不好,特别是百度根本没法抓取到SPA的内容页面,所以我们必须把我们的应用在服务端渲染成 ...