简体   繁体   English

如何将 CSV 文件中的 int 值存储在单独的二维数组元素中

[英]How do I store int values from CSV file in separate two-dimensional array elements

If I have a CSV file with 4 numbers on one line separated by a comma, how do I ignore the first two values using openCSV?如果我有一个 CSV 文件,一行中有 4 个数字,以逗号分隔,如何使用 openCSV 忽略前两个值?

Now, consider the following two-dimensional array:现在,考虑以下二维数组:

int[][] data = new int [10][10];

and the following line from a CSV file:以及来自 CSV 文件的以下行:

54, 68, 5, 1

assuming the former is possible (ignoring the first two values on a line in a CSV file), how do I then parse the value '5' into data[0][0] and parse the value '1' into data[0][1]?假设前者是可能的(忽略 CSV 文件中一行的前两个值),那么我如何将值 '5' 解析为 data[0][0] 并将值 '1' 解析为 data[0] [1]?

ie: IE:

data[0][0] = 5;
data[0][1] = 1;

Of course, if you cannot ignore the first two values, how do I parse each value (54, 68, 5, and 1) all in separate array elements?当然,如果你不能忽略前两个值,我如何解析每个值(54、68、5 和 1)都在单独的数组元素中?

ie: IE:

data[0][0] = 54;
data[0][1] = 68;
data[0][2] = 5;
data[0][3] = 1;

I can't find anything in the openCSV documentation that would explain how to do this, nor can I wrap my head around even doing it if there weren't a CSV file to read beforehand.我在 openCSV 文档中找不到任何可以解释如何执行此操作的内容,如果没有要事先阅读的 CSV 文件,我什至无法做到这一点。

Just googling "opencsv" got me to its project page .只是谷歌搜索“opencsv”让我进入了它的项目页面 That page has the following example:该页面有以下示例:

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
    // nextLine[] is an array of values from the line
   System.out.println(nextLine[0] + nextLine[1] + "etc...");
}

What prevents you from doing the same and just accessing the fields you want by index?是什么阻止你做同样的事情,只是通过索引访问你想要的字段?

data[0][0] = nextLine[2];
data[0][1] = nextLine[3];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用openCSV忽略CSV值+将CSV文件中的int值存储在单独的二维数组元素中 - Ignoring CSV values with openCSV + Storing int values from CSV file in separate two-dimensional array elements 从 CSV 读取值时,如何推入二维数组? - When reading values from a CSV, how to I push into a two-dimensional array? 如何将列表中的元素添加到二维数组? - How to add elements from a list to a two-dimensional array? 输入 int &lt;= 0 后,如何阻止二维 int 数组输出其值? - How do I stop a two dimensional int array from outputting its values once an int <= 0 is entered? 如何初始化二维数组? - How do I initialize a two-dimensional array? 如何调用从Java获取二维数组字符串的Clojure函数? - How do I call a Clojure function that takes a two-dimensional array of Strings from Java? 二维整数数组不接受我提供的尺寸 - Two-dimensional int array is not accepting the dimensions I am providing 如何检查数组是否是二维数组中的元素之一 - How to check if an array is one of the elements in a two-dimensional array 从二维数组(Java)顺序接收元素 - Sequential receiving of elements from the two-dimensional array (Java) 如何在Java的二维字符串数组中查找唯一元素? - How to find unique elements in a two-dimensional array of strings in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM