gpt4 book ai didi

java - 将行数组放入数组中

转载 作者:行者123 更新时间:2023-12-02 03:34:01 24 4
gpt4 key购买 nike

我有以下代码:

 try {
CsvParserSettings parserSettings = new CsvParserSettings();
parserSettings.setLineSeparatorDetectionEnabled(true);
RowListProcessor rowProcessor = new RowListProcessor();
parserSettings.setRowProcessor(rowProcessor);
parserSettings.setHeaderExtractionEnabled(true);
CsvParser parser = new CsvParser(parserSettings);

parser.parse(new FileReader("C:\\Users\\Desktop\\GeoIPCountryWhois.csv"));

String[] headers = rowProcessor.getHeaders();
List<String[]> rows = rowProcessor.getRows();
for (int index = 0;index< rows.size();index++){
System.out.println(Arrays.toString(rows.get(index)));
}

} catch (FileNotFoundException ex) {
Logger.getLogger(MyThread.class.getName()).log(Level.SEVERE, null, ex);
}

这每次都会创建一行数组像下面这样

[1.0.1.0, 1.0.3.255, 16777472, 16778239, CN, China]
[1.0.4.0, 1.0.7.255, 16778240, 16779263, AU, Australia]
[1.0.8.0, 1.0.15.255, 16779264, 16781311, CN, China]

但是我想将数据放入一个数组中,我该如何做到这一点?

最佳答案

你的意思是数组的数组吗?

List<String[]> rows = rowProcessor.getRows();
String[][] rowsArray = rows.toArray(new String[rows.size()][]); //this contains an array of all your rows

验证:

for (int i = 0; i < rowsArray.length; i++) {
System.out.println(Arrays.toString(rowsArray[i]));
}

这将提供与您相同的输出。

关于java - 将行数组放入数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37667427/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com