在Java列表的列表工作 [英] Working with a List of Lists in Java

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

问题描述

我是一个新手的Java程序员和具有[阵列]列出一些困难。具体来说,我想读一个CSV文件导入列表(字符串)的列表,围绕它传递了一个从数据库中获取一些数据,建立新的数据列表的一个新的列表,然后通过列表的那个列表,因此可以写入新的CSV文件。我已经看了遍,似乎无法找到这样的事情。我宁可不使用简单数组,因为这些文件将大小不一,我不知道用什么阵列的尺寸。我有处理文件没有问题。我只是不知道如何处理列表清单。

大多数我发现将创建多维数组或执行循环内部的从文件中读取数据的行动的例子。我知道我能做到这一点,但我想编写面向对象的code。如果你能提供一些例如code或点我的参考,这将是巨大的。

谢谢,
罗布


解决方案

 的ArrayList< ArrayList的<串GT;> listOLists =新的ArrayList< ArrayList的<串GT;>();
ArrayList的<串GT; singleList =新的ArrayList<串GT;();
singleList.add(你好);
singleList.add(世界);
listOLists.add(singleList);ArrayList的<串GT; anotherList =新的ArrayList<串GT;();
anotherList.add(这是另一个列表);
listOLists.add(anotherList);

I'm a newbie java programmer and having some difficulties with [Array]List. Specifically, 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 can't seem to find anything like that. 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.

Thanks, Rob

解决方案

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天全站免登陆