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就是线性代数中定义的矩阵和向量,所有的数学运算都和数学上一致.但是存在一个问题是数学上 ...
随机推荐
- dede列表页读取当前栏目名称
list或者arclist之内使用[field:typename/]之外使用{dede:field name='typename'/}
- 关于获取Windows下性能参数的总结
Windows下特定进程或者所有进程的CPU.物理内存.虚拟内存等性能参数的获取方法小结,包括如何在MFC中以及如何使用C#语言来获取参数. VC API:GlobalMemoryStatus 获取全 ...
- 如何让Ubuntu 12.04 LTS更炫更具吸引力
Ubuntu 12.04 LTS震撼发布 适逢七周岁生日之际,Ubuntu正式推出了第四个LTS长期支持版本,开发代号Precise Pangolin的Ubuntu 12.04在2012年4月26 ...
- Data Visualization Books
Data Visualization: Principles and Practice
- nginx用户认证配置( Basic HTTP authentication)及认证原理和实现
https://blog.csdn.net/guyue35/article/details/53906843
- 【LOJ】#2526. 「HAOI2018」苹果树
题解 这计数题多水啊我怎么调了那么久啊 我不想老年化啊QAQ (注意这里的二叉树带标号) 考虑\(g[i]\)表示\(i\)个点二叉树所有节点的深度和,\(f[i]\)表示\(i\)个点的二叉树两两节 ...
- Spark streaming的正确使用。。
转自http://bit1129.iteye.com/blog/2198531 代码如下: package spark.examples.streaming import java.sql.{Prep ...
- Orleans安装
一.Nuget包Orleans NuGet软件包从v1.5.0开始在大多数情况下,您需要使用4个关键的NuGet包: 1,Microsoft Orleans Build-time Code Gener ...
- Android Socket
Android Socket 参考资料 菜鸟教程 怎么理解TCP的面向连接和UDP的无连接 https://www.cnblogs.com/xiaomayizoe/p/5258754.html htt ...
- 表达式树(Expression Tree)
饮水思源 本文并非原创而是下面网址的一个学习笔记 https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/e ...