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就是线性代数中定义的矩阵和向量,所有的数学运算都和数学上一致.但是存在一个问题是数学上 ...
随机推荐
- Java基础84 javaBean规范
1.javaBean的概述 1.javaBeam(咖啡豆)是一种开发规范,也可以说是一种技术. 2.JavaBean就是一个普通java类,只要符合以下规定才能称作为javaBean: ...
- 第三届CCF软件能力认证
1.门禁系统 问题描述 涛涛最近要负责图书馆的管理工作,需要记录下每天读者的到访情况.每位读者有一个编号,每条记录用读者的编号来表示.给出读者的来访记录,请问每一条记录中的读者是第几次出现. 输入格式 ...
- appium入门级教程(2)—— 安装Appium-Server
前言 ==================== web自动化测试的路线是这样的:编程语言基础--->测试框架--->webdriver API--->开发自动化测试项目. 移动自动化 ...
- SprintBoot 1.2.8 入门
现在SpringBoot官网Quick Start的版本是1.5.3,试了一下,报错说我JDK版本太低,查了一下说是需要JDK8,所以我使用了旧版本1.2.8,实际上在POM中的依赖配置方式一样的. ...
- 【AtCoder】ARC089
C - Traveling 先看能不能走到,再看看奇偶性是否相同 #include <bits/stdc++.h> #define fi first #define se second # ...
- Java中public、protected、default和private的区别
public: 具有最大的访问权限,可以访问任何一个在classpath下的类.接口.异常等.它往往用于对外的情况,也就是对象或类对外的一种接口的形式. protected: 主要的作用就是用来保护子 ...
- 002.DNS-BIND简介
一 Linux-BIND服务器简介 Bind是Berkeley Internet Name Domain Service的简写,它是一款实现DNS服务器的开放源码软件.已经成为世界上使用最为广泛的DN ...
- 获取当前页面url中的参数 coffeescript+node.js+angular
获取当前url:@$window.alert @$location.url()获取参数(json格式)@$window.alert @$location.search().channel
- 区别ES3ES5和ES6this的指向问题。区分普通函数和箭头函数中this的指向问题
ES3 ES5this的指向问题 this指的是该函数被调用的对象 var foo = function () { this.a = 'a', this.b = 'b', this.c = { a: ...
- Python 訪问 LinkedIn (API)
CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-16 @author: guaguastd @name: l ...