java - 从 CSV 中检索 1 个值,然后将所有值单独添加到第一个值,无需列表

标签 java csv opencsv

我正在开发一个 Java 应用程序,我在其中将值存储到 CSV 中。我需要从 CSV 的一行中检索一个值,然后将 CSV 的该行中的所有值(包括该行)单独添加到第一个值。我需要对 CSV 的所有值执行此操作。例如:

假设我的 CSV 中有一行如下:

4
7
11
15

我想要执行以下操作:

4+7
4+11
4+15
7+11
7+15
11+15

我可以使用列表来执行此操作,但我想知道是否存在其他方法可以执行此操作,因为我不想将项目放入堆空间请注意:解决方案不必使用 OpenCSV,如果有任何方法可以做到这一点,我愿意倾听。我的代码如下:

CSVReader reader = null;
CSVReader reader1 = null;
try {
    reader = new CSVReader(new FileReader("F:\\Bignum\\bignum1.csv"),',', '\'', 1);
    reader1 = new CSVReader(new FileReader("F:\\Bignum\\bignum1.csv"),',', '\'', 1);
    String [] nextLin;

    for (int i = 0; i > 55; i++)
    {
        for (int k = 0 + 1; k > 81-i; k++)
        {
        }
    } 

最佳答案

像这样的事情怎么样:

CSVReader reader = new CSVReader(new FileReader("F:/Bignum/bignum1.csv"));
List<Integer> columnValues = new ArrayList<Integer>();
String[] row = null;
// read column 5 values into list
while ((row = reader.readNext()) != null){
    columnValues.add(Integer.parseInt(row[4]));
}
Integer[] numberArray = columnValues.toArray(new Integer[columnValues.size()]);
// iterate over array
for (int a = 0; a < numberArray.length; a++){
    for (int b = a+1; b < numberArray.length; b++){
        int aa = numberArray[a];
        int bb = numberArray[b];
        System.out.println(aa+" + "+bb+" = "+(aa+bb));
    }
}

给予:

4 + 7 = 11
4 + 11 = 15
4 + 15 = 19
7 + 11 = 18
7 + 15 = 22
11 + 15 = 26

关于java - 从 CSV 中检索 1 个值,然后将所有值单独添加到第一个值,无需列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20084398/

相关文章:

java - 打开csv集合和连续数组

java - 如何缓存 Spring 应用程序上下文以便在许多 JVM 中使用它

JAVA 检查字符串的特定格式?

java - 如何通过反射改变方法行为?

csv - AWK - 处理大型 CSV(130 亿行)以获取基于多列(复合键)的重复数据会导致内存不足错误

PHP 到 CSV - 将一行分成多行

python - 使用 Tweepy 从 Twitter 获取数据并存储在 csv 文件中

java - 如何以编程方式获取LogBack配置文件路径/名称?

java - Opencsv解析中的自定义逻辑

scala - 关闭!!我如何从 Scala 中的字符串中检测类型?