Java 二维数组转换成List<List<String>>[通俗易懂]

Java 二维数组转换成List<List<String>>[通俗易懂]在拿到二维数组的数据时,需要将它转换成嵌套的list,如下工具类:importjava.util.ArrayList;importjava.util.List;/***将二维数组转换成List>形式工具**@authorqiulinhe**2017年3月1日下午3:09:52*/publicclassArrayToListTe

大家好,又见面了,我是你们的朋友全栈君。

          在拿到二维数组的数据时,需要将它转换成嵌套的list,如下工具类:

import java.util.ArrayList;
import java.util.List;

/**
 * 将二维数组转换成List<List<String>>形式工具
 * 
 * @author qiulinhe
 *
 *         2017年3月1日 下午3:09:52
 */
public class ArrayToListTest {

	public static void main(String[] args) {
		String[][] strCe = new String[2][2];
		strCe[0][0] = "1";
		strCe[0][1] = "2";
		strCe[1][0] = "3";
		strCe[1][1] = "4";

		List<List<String>> listTest = new ArrayList<List<String>>();
		for (int i = 0; i < strCe.length; i++) {
			List<String> columnList = new ArrayList<String>();
			for (int j = 0; j < strCe[i].length; j++) {

				columnList.add(j, strCe[i][j]);

			}
			listTest.add(i, columnList);
		}

		System.out.println(listTest);
		System.out.println(strCe);

	}
}

          如果你是要传接送给后台的话,也可以直接使用fastjson进行转换:

	/**
	 * 将二维数组转换成List<List<String>>形式工具
	 * 
	 * @param retireStringArray
	 *            excel拼接的字段内容
	 * @return
	 */
	public static List<List<String>> parseStringToList(String[][] retireStringArray) {

		// List<List<String>> listTest = new ArrayList<List<String>>();
		// for (int i = 0; i < retireStringArray.length; i++) {
		// List<String> columnList = new ArrayList<String>();
		// for (int j = 0; j < retireStringArray[i].length; j++) {
		//
		// columnList.add(j, retireStringArray[i][j]);
		//
		// }
		// listTest.add(i, columnList);
		// }
		// return listTest;

		// 使用fastjson进行转换
		List<List<String>> lists = new ArrayList<List<String>>();
		lists = (List<List<String>>) JSON.parseObject(JSON.toJSONString(retireStringArray),
				new TypeReference<List<List<String>>>() {
				});
		return lists;

	}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/151734.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Git下载安装手把手教程[通俗易懂]

    Git下载安装手把手教程[通俗易懂]Git(读音为/gɪt/)是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理。下面我们来详细介绍下,在windows10系统下载和安装git的教程,文章结尾有福利哦!Git安装手把手安装教程:第一步:下载Git打开Git官网下载地址https://git-scm.com/downloads因为我使用Windows系统上的浏览器访问的,Git官网自动之别到了我使用的操作系统.

    2022年5月30日
    36
  • 使用adb安装apk命令格式

    使用adb安装apk命令格式adbinstall[-r][-s]-r表示重新安装APK包,-s表示将APK包安装到SD卡上adbinstall[-k]-k表示只删除应用程序,但保留该程序所用的数据和缓存目录

    2022年5月18日
    45
  • MySQL安装出问题

    MySQL安装出问题

    2020年11月9日
    216
  • SpringCloud和dubbo的区别[通俗易懂]

    SpringCloud和dubbo的区别[通俗易懂]SpringCloud跟dubbo的区别从架构层面上来说SpringCloud跟dubbo都是微服务架构在公司开发技术选型中:SpringCloud的维护成本比较高,但是SpringCloud中提供了很多框架、整合了5大组件(全家桶:Ribbon负载均衡、eureka注册中心、Hystrix熔断器、gateway网关、feign服务调用)使用都非常方便,后期便于维护,分布式单一互不影响原则…

    2022年4月30日
    68
  • 汉字转拼音 文字集

    汉字转拼音 文字集publicclassChEnRow{publicstring[]enfull;}privatestaticList<ChEnRow>CEFullTable=newList<ChEnRow>{newChEnRow{enfull=newstring[]{“a,啊阿呵吖嗄腌锕錒”,”ai…

    2022年6月21日
    23
  • MongoDB 索引详解

    MongoDB 索引详解索引能够提高数据库的查询效率,没有索引的话,查询会进行全表扫描(scan every document in a collection),严重降低了查询效率,故学会使用索引将是一项重要技能。

    2022年6月17日
    25

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号