在 Java 中使用列表列表 [英] Working with a List of Lists in Java

查看:33
本文介绍了在 Java 中使用列表列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 CSV 文件读入(字符串)列表列表中,将其传递以从数据库中获取一些数据,构建新数据列表的新列表,然后传递该列表列表它可以写入新的 CSV 文件.我已经看遍了所有内容,但似乎无法找到有关如何执行此操作的示例.

I'm trying to read a CSV file into a list of lists (of strings), pass it around for getting some data from a database, build a new list of lists of new data, then pass that list of lists so it can be written to a new CSV file. I've looked all over, and I can't seem to find an example on how to do it.

我宁愿不使用简单的数组,因为文件的大小会有所不同,而且我不知道数组的维度应该使用什么.我在处理文件方面没有问题.我只是不确定如何处理列表列表.

I'd rather not use simple arrays since the files will vary in size and I won't know what to use for the dimensions of the arrays. I have no issues dealing with the files. I'm just not sure how to deal with the list of lists.

我发现的大多数示例都将创建多维数组或在从文件读取数据的循环内执行操作.我知道我可以做到,但我想编写面向对象的代码.如果您能提供一些示例代码或指向我的参考,那就太好了.

Most of the examples I've found will create multi-dimensional arrays or perform actions inside the loop that's reading the data from the file. I know I can do that, but I want to write object-oriented code. If you could provide some example code or point me to a reference, that would be great.

推荐答案

ArrayList<ArrayList<String>> listOLists = new ArrayList<ArrayList<String>>();
ArrayList<String> singleList = new ArrayList<String>();
singleList.add("hello");
singleList.add("world");
listOLists.add(singleList);

ArrayList<String> anotherList = new ArrayList<String>();
anotherList.add("this is another list");
listOLists.add(anotherList);

这篇关于在 Java 中使用列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆