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. KnockoutJs学习笔记(七)

    if binding与visible binding类似.不同之处在于,包含visible binding的元素会在DOM中一直保存,并且该元素相应的data-bind属性会一直保持,visible ...

  2. 项目通过https访问的tomcat相关配置

    开发项目已经完成,那么就是要部署项目到服务器上面.我最近把刚完成的项目部署到服务器上面,内网通过http协议进行访问一切正常,但是测试外网通过https协议进行访问的时候就出现了一些js文档找不到的b ...

  3. 【LOJ】#2479. 「九省联考 2018」制胡窜

    题解 老了,国赛之前敲一个后缀树上LCT和线段树都休闲的很 现在后缀树上线段树合并差点把我写死 主要思路就是后缀树+线段树合并+容斥,我相信熟练的OIer看到这已经会了 但就是不想写 但是由于我过于老 ...

  4. LOOPS 概率dp

    题意:迷宫是一个R*C的布局,每个格子中给出停留在原地,往右走一个,往下走一格的概率,起点在(1,1),终点在(R,C),每走一格消耗两点能量,求出最后所需要的能量期望 简单概率dp 注意   原地不 ...

  5. 002.iSCSI服务端配置

    一 iSCSI target的磁盘种类 大型文件 单一分区(partition) 磁盘 数组 RAID LVM 二 iSCSI创建步骤 建立用于共享的磁盘设备(分区/磁盘/文件) 创建后备磁盘 创建相 ...

  6. [代码审计]phpshe开源商城后台两处任意文件删除至getshell

    0x00 背景 这套系统审了很久了,审计的版本是1.6,前台审不出个所以然来.前台的限制做的很死. 入库的数据都是经过mysql_real_escape_string,htmlspecialchars ...

  7. Android- SharedPreferences 使用详解

    Android-SharedPreferences 使用详解 参考 https://developer.android.google.cn/reference/android/content/Shar ...

  8. Android-RemoteView-桌面小部件

    Android-RemoteView-桌面小部件 学习自 <Android开发艺术探索> https://developer.android.google.cn/guide/topics/ ...

  9. require和require.async的区别

    本文用seajs来讲解两种模块加载方式require和require.async的区别,类似java里的import,php里的include. <!DOCTYPE html> <h ...

  10. 关于ListView中EditText在软键盘弹出后的焦点问题

    转自:http://www.cnblogs.com/haofei/p/3305030.html 在ListView中,每次弹出软键盘后就会重新调用getView()方法,导致EditText失去焦点. ...