Java基础之数组与List之间的互相转换(常见的几种方式,一看就懂)

article/2025/9/14 6:08:14

Java基础之数组与List之间的互相转换

  • 1. List转数组
    • 1.1 方式一:toArray()
    • 1.2 方式二:list.toArray(new String[list.size()])
    • 1.3 方式三:list.stream().toArray()
    • 1.4 方式四:list.stream().toArray(String[]::new);
    • 1.5 方式五:
  • 2. 数组转List
    • 2.1 方式一:Arrays.asList(strS)
    • 2.2 方式二:new ArrayList<>(Arrays.asList(strS))
    • 2.3 方式三:使用 Stream
    • 2.4 方式四:Collections.addAll(list,strS);
  • 3. 附测试代码
    • 3.1 list 转数组的代码
    • 3.2 数组转list的代码
  • 4. 另:数组和集合的遍历

1. List转数组

这些东西都是简单易懂,就是容易忘记,所以下面介绍都是直接给代码

1.1 方式一:toArray()

在这里插入图片描述

1.2 方式二:list.toArray(new String[list.size()])

在这里插入图片描述
在这里插入图片描述

1.3 方式三:list.stream().toArray()

在这里插入图片描述

1.4 方式四:list.stream().toArray(String[]::new);

  • 下面的 String[]::new 这种写法是Java8的新特性,不明白的自己可以下去看看Java新特性,下面我们的方式五是替换这种方法,方便大家的理解
    在这里插入图片描述

1.5 方式五:

  • 这种方式很少用,我们写出来主要是为了理解:上面方式四 list.stream().toArray(String[]::new); 的这种写法,看完之后应该明白,String[]::new 其实是调用了构造方法,还不是很清楚的,自己下去看看源码再了解一下Lambda表达式就明白了。
    在这里插入图片描述
    在这里插入图片描述

2. 数组转List

2.1 方式一:Arrays.asList(strS)

  • 需要注意:不能对List进行增删操作
    在这里插入图片描述

2.2 方式二:new ArrayList<>(Arrays.asList(strS))

在这里插入图片描述

2.3 方式三:使用 Stream

在这里插入图片描述

2.4 方式四:Collections.addAll(list,strS);

在这里插入图片描述

3. 附测试代码

3.1 list 转数组的代码

package com.liu.susu.base.coollection.array;import org.junit.Test;import java.util.ArrayList;
import java.util.List;
import java.util.function.IntFunction;/*** @FileName example1* @Description* @Author susu* @date 2022-03-01**/
public class ListToArray {/*** 方式一:Object[] result = arrayList.toArray();* 对结果 Object[] 不能强转*/@Testpublic void listToArrayTest1(){ArrayList arrayList = new ArrayList();arrayList.add("aa");arrayList.add("cc");arrayList.add("dd");Object[] result = arrayList.toArray();//注意此处不能向下强转成Stringfor (int i = 0; i < result.length; i++) {System.out.println(result[i]);}/********************下面强转报错,返回结果是Object[],不能强转***************************///java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;/*String[] array = (String[]) arrayList.toArray();for (String s : array) {System.out.println(s);}*/}/*** 方式二(分写):*         String[] strings = new String[list.size()];*         list.toArray(strings);*/@Testpublic void listToArrayTest2_1(){List<String> list = new ArrayList<>();list.add("aa");list.add("cc");list.add("dd");String[] strings = new String[list.size()];list.toArray(strings);for (String s : strings) {System.out.println(s);}}/*** 方式二(简写):String[] strings = list.toArray(new String[list.size()]);*/@Testpublic void listToArrayTest2_2(){List<String> list = new ArrayList<>();list.add("aa");list.add("cc");list.add("dd");String[] strings = list.toArray(new String[list.size()]);for (int i = 0; i < strings.length; i++) {System.out.println(strings[i]);}}/*** 方式三:Object[] objects = list.stream().toArray();* 对结果 Object[] 不能强转*/@Testpublic void listToArrayTest3(){List<String> list = new ArrayList<>();list.add("aa");list.add("cc");list.add("dd");Object[] objects = list.stream().toArray();for (Object object : objects) {System.out.println(object);}}/*** 方式四:String[] strings = list.stream().toArray(String[]::new);*/@Testpublic void listToArrayTest4(){List<String> list = new ArrayList<>();list.add("aa");list.add("cc");list.add("dd");String[] strings = list.stream().toArray(String[]::new);for (String s : strings) {System.out.println(s);}}/*** 方式五:对上述方式四的解释*/@Testpublic void listToArrayTest5(){List<String> list = new ArrayList<>();list.add("aa");list.add("cc");list.add("dd");String[] strings2 = list.stream().toArray(new IntFunction<String[]>() {@Overridepublic String[] apply(int value) {return new String[list.size()];}});for (String s : strings2) {System.out.println(s);}System.out.println("==========================================");String[] strings3 = list.stream().toArray(new IntFunction <String[]>() {@Overridepublic String[] apply(int value) {return new String[value];}});for (String s : strings3) {System.out.println(s);}}}

3.2 数组转list的代码

package com.liu.susu.base.coollection.array;import org.junit.Test;import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;/*** @FileName ArrayToList* @Description* @Author susu* @date 2022-03-01**/
public class ArrayToList {@Test/*** 方式一:Arrays.asList(strS)* Arrays.asList(strS)通过这种方式将数组转换为List后,只能作为数据源读取使用* 不能进行增删操作*/public void arrayToListTest1(){String[] strS = {"aa","bb","cc"};List<String> stringList = Arrays.asList(strS);
//        stringList.add("dd");  //不能对List增删,只能查改,否则抛异常。for (String s : stringList) {System.out.println(s);}}/*** 方式二:new ArrayList<>(Arrays.asList(strS))* 通过ArrayList的构造器创建:new ArrayList<>(Arrays.asList(strS))* 支持增删操作* 备注:对List进行增删改查操作,在List的数据量不大的情况下,可以使用*/@Testpublic void arrayToListTest2(){String[] strS = {"aa","bb","cc"};ArrayList<String> list = new ArrayList<>(Arrays.asList(strS));list.add("dd");//支持增删操作for (String s : list) {System.out.println(s);}}/*** 方式三Java8:Stream.of(strS).collect(Collectors.toList())* 支持增删操作*/@Testpublic void arrayToListTest3(){String[] strS = {"aa","bb","cc"};List<String> stringList = Stream.of(strS).collect(Collectors.toList());stringList.add("ff");stringList.forEach(System.out::println);}/*** 方式四Java8:Collections.addAll(list,strS);* 支持增删操作* 备注:对List进行增删改查操作,在List的数据量巨大的情况下,优先使用,可以提高操作速度。*/@Testpublic void arrayToListTest4(){String[] strS = {"aa","bb","cc"};
//        List<String> list = new ArrayList<>(strS.length);List<String> list = new ArrayList<>();Collections.addAll(list,strS);list.add("mm");for (String s : list) {System.out.println(s);}}}

4. 另:数组和集合的遍历

Java基础之常见遍历方法(一看就懂).


http://chatgpt.dhexx.cn/article/4VTBD93u.shtml

相关文章

Intel MKL库在VS中的配置与使用

转载自https://blog.csdn.net/world_6520/article/details/84959233 自己留一波以防原作删除 主要是手动配置&#xff08;一般编译器会自动配置好&#xff0c;如果没有配置&#xff0c;按如下步骤进行&#xff09;&#xff1a;新建一个c项目&#xff0c;打开属性管理器&#x…

[Eigen中文文档] 在 BLAS/LAPACK 、英特尔® MKL 和 CUDA 中使用 Eigen

文档总目录 本文目录 在BLAS/LAPACK使用 Eigen在英特尔 MKL使用 Eigen链接 在 CUDA 内核中使用 Eigen 在BLAS/LAPACK使用 Eigen 英文原文(Using BLAS/LAPACK from Eigen) 自Eigen 3.3版本以及以后&#xff0c;任何F77兼容的BLAS或LAPACK库都可以用作稠密矩阵乘积和稠密矩阵分…

Intel® MKL-DNN

Intel MKL-DNN 在现代英特尔架构中&#xff0c;缓存和内存使用效率会对整体性能产生显著影响。良好的内存访问模式可以最大限度地降低访问内存数据的额外成本&#xff0c;不会降低整体处理速度。若要实现这一目标&#xff0c;数据的存储和访问方式起着重要作用。这通常被称为数…

MKL学习——向量操作

前言 推荐两个比较好的教程: BLAS (Basic Linear Algebra Subprograms) LAPACK for Windows 命名规范 BLAS基本线性代数子程序的函数命令都有一定规范&#xff0c;便于记忆 <character> <name> <mod> () character 定义的是数据类型 s实数域&#…

mkl简介

一、概况 &#xff08;一&#xff09;下载 下载免费版本&#xff0c;注意保留serial number&#xff08;安装需要&#xff09; &#xff08;二&#xff09;安装 编辑silent.cfg 文件中的选项安装需要2G的空间&#xff0c;默认的tmp空间不足使用**–tmp_dir**指定 ./instal…

如何解决kaldi的依赖库mkl安装失败的问题

最近在学习如何使用kaldi进行语音识别。按照进程进行安装部署时发现Intel MKL库总是失败。 通过搜索大量的资料&#xff0c;但都发现不太适用。现在将失败的症状和解决方法分享一下&#xff0c;希望能给读者提供一些帮助。 通过执行 ./check_dependencies.sh 发现缺少Intel M…

oneKey mkl安装

1、oneKey mkl核心安装 进入官方下载地址&#xff0c;采用离线安装方式 1&#xff09;选择离线安装 2&#xff09;可以直接点击“download”下载安装包后再解压安装&#xff08;此种方式需要注册帐号&#xff0c;比较麻烦&#xff0c;介意选择下面的命令行安装&#xff09; …

Linux下MKL库的安装部署与使用,并利用cmake编译器调用MKL库去提升eigen库的计算速度

文章目录 前言一、MKL库的下载二、MKL库的安装与配置1.MKL库的安装与配置2.代码测试 总结 前言 在用C/C编写模型预测控制算法(MPC)的代码时候&#xff0c;由于预测步长和控制步长的设置较大&#xff0c;导致在利用eigen库进行矩阵计算的时候&#xff0c;矩阵n次幂计算时间过长&…

mysql 报1055错误_MySQL数据库报1055错误

有点坑啊&#xff0c;当初装MySQL数据库的时候没有整配置文件&#xff0c;结果MySQL报1055错误的时候&#xff0c;网上的解决办法都说如果需要永久生效的话&#xff0c;只能通过改配置文件实现&#xff0c;but&#xff0c;我没有配置文件&#xff0c;蜜汁尴尬啊 1、已安装的MyS…

mysql运行sql错误1055_sql数据库执行错误代码1055怎么解决?

展开全部 错误代码1055。不过看了Expression里面的原因描述,我基本知道怎么回事了。原因是因为62616964757a686964616fe59b9ee7ad9431333431373139mysql中对 group by 用法的规定。严格意义上说,就是group by之后。select 的字段只能是group by的字段。或者需要加聚合函数的。…

FOJ 1055

一&#xff0c;题目链接 http://acm.fzu.edu.cn/problem.php?pid1055 二&#xff0c;题目描述 三&#xff0c;题目分析 1.程序段的格式是已知的&#xff0c;一行为三个字符&#xff0c;且中间是赋值运算符&#xff0c;只需用一个数组记录字符是否 是已知的&#xff08;查表法…

MYSQL数据库报错 1055

MYSQL数据库报错 1055 今天在做毕业设计&#xff0c;当点开要查看的视图的时候&#xff0c;爆出了1055的错&#xff0c;该错误如下&#xff1a; 错误原因&#xff1a;在MySQL5.7之后&#xff0c;sql_mode中默认存在ONLY_FULL_GROUP_BY&#xff0c;SQL语句未通过ONLY_FULL_GRO…

leetcode1055

暴力查询即可&#xff0c;外面一层遍历target字符串&#xff0c;内层对source进行遍历&#xff0c;直到当前的内层遍历source中已无可匹配字符&#xff0c;在进行下一次source遍历&#xff0c;直到外层遍历结束。最终统计进行了几次内层遍历。 优化的方法&#xff1a;将内层遍历…

Mysql报错1055

Mysql group by报错 1055 [Err] 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘xm_xhd.al.dy_uid’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_modeonly_full_…

MySQL 报错1055

一、问题描述 SELECT * FROM tbluser GROUP BY sex当我以这条语句进行数据库查询的时候&#xff0c;报了个错&#xff1a; > 1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ceb.tbluser.uid which is not functiona…

mac mysql 1055_MySQL错误1055

问题描述:在MySQL数据库下,执行SQL插入语句报错。错误信息如下: 错误原因:在MySQL5.7之后,sql_mode中默认存在ONLY_FULL_GROUP_BY,SQL语句未通过ONLY_FULL_GROUP_BY语义检查所以报错。 ONLY_FULL_GROUP_BY:ONLY_FULL_GROUP_BY要求select语句中查询出来的列必须是明确的(…

MySQL 错误代码:1055 解决方案(推荐!!)

MySQL 错误代码:1055 解决办法 一、 MySQL版本二、 问题描述1. 问题描述2.ONLY_FULL_GROUP_BY-SQL示例 三、解决办法1.方法一2.方法二&#xff08;临时&#xff09;3.方法三&#xff08;永久&#xff09; 四、sql_mode常用值 一、 MySQL版本 MySQL版本&#xff1a;8.0.11 二、…

MySQL 出现1055错误 this is incompatible with sql mode=only full group by 的解决办法

文章目录 一、打开MySQL8.0 Command Line Client二、找到MySQL的my.ini文件路径三、修改my.ini里面的内容 前言 在学习过程中出现的问题&#xff1a;IDEA与Navicat出现不兼容的情况 会弹出1055的错误&#xff0c;这种错误通常在mysql 5.7以上的版本才会出现 1055 Expression #…

MySQL查询出现1055错误的解决方法

当SQL语句查询报1055错误时的解决方法 报错内容如下 报错原因 1.SQL语句中使用了group by&#xff0c;并且不需要分组的字段没有加上any_value()函数 2.MySQL数据库版本是大于5.7&#xff0c;报错信息中最后有一句sql_modeONLY_FULL_GROUP_BY&#xff0c;是因为MySQL数据库的…

1055 习题4-9-3 逆序输出正整数各位上数字

题目描述 输入一个不多于5位的正整数&#xff0c;按逆序输出各位上的数字&#xff0c;末尾换行。 注意&#xff1a;确保输入的正整数的位数不多于5。 输入 一个不多于5位的正整数。 输出 逆序输出各位上的数字&#xff0c;中间以空格分隔。 注意末尾的换行。 样例输入 2143 样…