java - 如何在java中打印List<List<String[]>>的值?

标签 java arrays list csv

我想从列表中获取各个数组的值并利用它们

 import java.io.FileReader;   
 import java.util.ArrayList; 
 import java.util.List;
 import au.com.bytecode.opencsv.CSVReader;


public class ReadCSV {

public static void main(String[] args) {

    String startFile = "/Users/ray/Downloads/hello.csv";
    //String outFile = "./outData.xml";

    try {
        CSVReader reader = new CSVReader(new FileReader(startFile));
        String[] line = null;

        String[] header = reader.readNext();

        List<List<String[]>> out = new ArrayList<List<String[]>>();

        while((line = reader.readNext())!=null){
            List<String[]> item = new ArrayList<String[]>();
                for (int i = 0; i < header.length; i++) {
                String[] keyVal = new String[2];
                String string = header[i];
                System.out.println("the value of the header : "+string);
                String val = line[i];
                System.out.println("the value of the field : "+val);
                keyVal[0] = string;
                keyVal[1] = val;
                item.add(keyVal);
            }
            out.add(item);
        }


    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
  }

我有一个以下格式的 csv 文件:-

Keyword,AlternateKeywords---> these are the header fields
apple,banana
orange,ego kit
ego ce4,venus
demo,cat

我想要以下形式的数组:

array[] keyword={apple,orange,ego ce4,demo}
array[] banana={banana,ego kit,venus,cat}

我不知道如何从列表“out”中获取数据并获取值并打印上面的元素。它可以是任意数量的标题以及该特定列的任意数量的元素,上面的 csv 只是一个示例。

如果可能的话请帮助我。

最佳答案

看看你的结构:

array[] keyword={apple,orange,ego ce4,demo}
array[] banana={banana,ego kit,venus,cat}

您不需要像 List<List<String[]>> 这样的 3D但只有 2D List<List<String>>

这是工作代码

public static void main(String[] args) {

    String startFile = "C:\\workspacePrototype\\some.csv";

    try {
        CSVReader reader = new CSVReader(new FileReader(startFile));
        String[] line = null;

        String[] headers = reader.readNext();


        List<List<String>> build = new ArrayList<List<String>>();

        List<String> tempArr;

        // generate headers
        for(String header : headers){
            tempArr = new ArrayList<String>();
            tempArr.add(header);
            build.add(tempArr);
        }


        // generate content
        while((line = reader.readNext())!=null){

            for (int i = 0; i < build.size(); i++) {
                tempArr = build.get(i);
                String val = line[i];
                tempArr.add(val);
                build.set(i, tempArr);
            }

        }

        System.out.println(Arrays.asList(build));


    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

输出:

 [[[Keyword, apple, orange, ego ce4, demo], [AlternateKeywords, banana, ego kit, venus, cat]]]

[编辑]

顺便说一句,您可以创建一些类,例如:

 public class Column{
  private String mName;
  private List<String> mData;

  // get/set
 }

相反

 List<List<String>> build = new ArrayList<List<String>>();

使用如下:

 List<Column> build = new ArrayList<Column>();

这样Column类应将列名存储为 mName并将数据发送至 mData

关于java - 如何在java中打印List<List<String[]>>的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19020519/

相关文章:

java - 如何在数组中使用 toString

java - Google 的 GSON 是否使用构造函数?

arrays - 读取 CSV 文件并保存在二维数组中

python-3.x - 如何将列表中的元素作为索引传递给Python中的列表?

c# - 我在 C# 中有两个列表 Found Missed Element count 和 Founded Element Count

java - 集合仍然存在,但没有出现在另一面

java - 内联初始化的最终成员变量不知何故为空

java - 难以理解多维数组

c - 如何返回每个函数实例大小不同的 VLA?

Python 创建列表字典