List与Array之间互换
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 implementsRandomAccess.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中提供了一种使用的方法。更为常用的示例代码:
- String[] arr = new String[] {"str1", "str2"};
- 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:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceList<E>- Overrides:
toArrayin classAbstractCollection<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:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceList<E>- Overrides:
toArrayin classAbstractCollection<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 listNullPointerException- if the specified array is null
用法:示例代码:
- List<String> list = new ArrayList<String>();
- list.add("str1");
- list.add("str2");
- int size = list.size();
- String[] arr = (String[])list.toArray(new String[size]);//使用了第二种接口,返回值和参数均为结果
List与Array之间互换的更多相关文章
- JAVA中List与Array之间互换
1.Array转List ArrayList<String> list = new ArrayList<String>(); String[] arr = new String ...
- [Swift]JSON字符串与字典(Dictionary)、数组(Array)之间的相互转换
1.JSON字符串与字典(Dictionary)之间的相互转换 import Foundation //JSON字符串转换为字典(Dictionary) func getDictionaryFromJ ...
- Python PIL 的image类和numpy array之间的互换
import cv2 import numpy as np from PIL import Image from PIL import ImageEnhance def getline(frame): ...
- ArrayList和Array之间的转换
ArrayList转Array (1):使用ArrayList的toArray方法. 1)当ArrayList中存放的是引用类型时(例如String),成功 /** * 使用 ...
- Java-Collection、Map及Array之间的转换
1 List -> Map 设个User类: public class User { private String userName; private String userId; privat ...
- Java-Collection、Map和Array之间的转换
1 List -> Map 设个User类: public class User { private String userName; private String userId; privat ...
- 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... ...
- Vector, ArrayList, Array
JAVA新手在使用JAVA的时候大概都会遇到这个问题: JAVA中的Array, ArrayList, Vector, List, LinkedList有什么样的区别?尤其是Vector, Array ...
- Eigen学习之Array类
Eigen 不仅提供了Matrix和Vector结构,还提供了Array结构.区别如下,Matrix和Vector就是线性代数中定义的矩阵和向量,所有的数学运算都和数学上一致.但是存在一个问题是数学上 ...
随机推荐
- 2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1 20165301
2018-2019-2 <网络对抗技术>Exp0 Kali安装 Week1 20165301 安装kali 参考此网站 设置共享文件夹 虚拟机->设置->选项->共享文件 ...
- ERP合同管理(二十七)
需要实现的基本业务: 相关的用例图: 相关业务的封装: 相关的约定: 合同信息添加业务流程: 添加的存储过程 SET QUOTED_IDENTIFIER ON SET ANSI_NULLS ON GO ...
- 使用div模拟出frameset效果
<!doctype html> <html xmlns="http://www.w3.org/1999/xhtml" > <head> < ...
- spark优化之临时目录
官方是这样说的: Directory to use for "scratch" space in Spark, including map output files and RDD ...
- 利用 gdb 探究main(int argc, char *argv[]){} 中的char *argv[]
在 Linux 系统中编写小程序 代码如下 编译并采用gdb调试 在调试之前设置三个参数 a bb ccc 输入 start 执行代码到 return 0; 从这里可以看到 argc = ...
- ZOJ Monthly, March 2018 题解
[题目链接] A. ZOJ 4004 - Easy Number Game 首先肯定是选择值最小的 $2*m$ 进行操作,这些数在操作的时候每次取一个最大的和最小的相乘是最优的. #include & ...
- 【原创】MySQL Can't create a new thread报错分析
今天有两台服务器都出现了Can't create a new thread报错. [故障处理过程] 故障发生后登录服务器,检查mysql进程正常,但登录mysql报下面错误 ERROR 1135 (H ...
- 学习字典才联想到要和 JSP 说再见了
最开始只是想让页面能够映射出我的字典值,然而却发现了更大的问题. 假如你自己做一个 demo ,需要前台页面映射出字典数据你会怎么做呢?大致的思路应该是有的,准备字典,准备数据,然后将两部分进行映射. ...
- 电子邮件-TCP
参考下图,由于电子邮件是最早应用于网络上的服务,而早期网络的不可靠性,才设计了TCP协议,而邮件必须要保证正确传输,而非高速,所以早期的电子邮件协议 全是基于TCP的,一直持续到现在.
- Android-多进程初识
Android-多进程初识 学习自 <Android开发艺术探索> https://baike.baidu.com/item/%E8%BF%9B%E7%A8%8B/382503?fr=al ...