Java 从CSV中检索1个值,然后将所有值分别添加到第一个值中,无需列表

Java 从CSV中检索1个值,然后将所有值分别添加到第一个值中,无需列表,java,csv,opencsv,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 我想做以下几点: 4 7 11 15 4+7 4+11 4+15 7+11 7+15 11+15 我可以使用列表来完成此操作,但我想知道是否存在其他方法来完成此操作,因为我不想将项目放入堆空间

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

假设我的CSV中有一行是:

4
7
11
15
4+7
4+11
4+15
7+11
7+15
11+15
我想做以下几点:

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++)
        {
        }
    } 

OpenCSV似乎只有两个选项,一个是通过读取一行来创建字符串[],另一个是通过读取整个文件来创建列表。我不确定为什么要以这种方式设置
for
-循环,因为它看起来不清楚应该查看什么

    int arr[] = new int[nextLin.length]; //I'm assuming nextLin has your CSV values.

    for (int i = 0 ; i < nextLin.length; i++){ 
       int x = Integer.parseInt(nextLin[i]); 
       for(int j = i+1 ; j < nextLin.length; j++){ 
          System.out.println(x + Integer.parseInt(nextLin[j]));
       }
    }
intarr[]=newint[nextLin.length]//我假设nextLin有你的CSV值。
对于(int i=0;i
我真的不认为有必要这样做,除非您希望递归地这样做(这似乎是不必要的):

public void mergeAdd(字符串[]lin,int-min,int-max){
如果(最大<最小)返回;
//最小值和最大值分别为0和lin.length-1
如果(min==max){//基本情况
intx=Integer.parseInt(lin[min]);
对于(int i=min+1;i
再说一次,我不知道你为什么要寻找另一种方法,但这只是两种方法


编辑:修复了一些问题。现在输出应该是正确的。您可以将其从
System.out.println
更改为您希望输出的位置。

看起来OpenCSV只有两个选项,即通过读取一行来创建字符串[],或通过读取整个文件来创建列表。我不确定为什么要以这种方式设置
for
-循环,因为它看起来不清楚应该看什么

    int arr[] = new int[nextLin.length]; //I'm assuming nextLin has your CSV values.

    for (int i = 0 ; i < nextLin.length; i++){ 
       int x = Integer.parseInt(nextLin[i]); 
       for(int j = i+1 ; j < nextLin.length; j++){ 
          System.out.println(x + Integer.parseInt(nextLin[j]));
       }
    }
intarr[]=newint[nextLin.length]//我假设nextLin有你的CSV值。
对于(int i=0;i
我真的不认为有必要这样做,除非您希望递归地这样做(这似乎是不必要的):

public void mergeAdd(字符串[]lin,int-min,int-max){
如果(最大<最小)返回;
//最小值和最大值分别为0和lin.length-1
如果(min==max){//基本情况
intx=Integer.parseInt(lin[min]);
对于(int i=min+1;i
再说一次,我不知道你为什么要寻找另一种方法,但这只是两种方法


编辑:修复了一些问题。现在输出应该是正确的。您可以将其从
System.out.println
更改为您希望输出的位置。

这样的内容怎么样:

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));
    }
}

像这样的东西怎么样:

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));
    }
}

那么,你不想在内存中存储任何东西?下面的解决方案将直接从文件中读取值,但每次需要获取值时都会重新读取文件。警告:此工作负载(即读取文件)将以指数形式增大(?)行数越多

此解决方案不使用CSVReader,它只是使用逗号分隔符解析csv行。您可以轻松地修改此解决方案以使用CSVReader

import java.io.*;

public class Main {

    public static void main(String[] args) throws Exception {
        File file = new File("F:/Bignum/bignum1.csv");
        // get total number of lines
        int totalRows = getTotalRows(file);
        int column = 4;
        for (int a = 0; a < totalRows; a++){
            int aa = getCellValue(file,a,column);
            for (int b = a+1; b < totalRows; b++){
                int bb = getCellValue(file,b,column);
                System.out.println(aa+" + "+bb+" = "+(aa+bb));
            }
        }
    }

    public static int getTotalRows(File file) throws Exception {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        int totalLines = 0;
        while (reader.readLine() != null) totalLines++;
        reader.close();  
        return totalLines;
    }

    public static int getCellValue(File file, int row, int column) throws Exception {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        int currentLine = 0;
        String line = null;
        while (currentLine++ < row) line = reader.readLine();
        return Integer.parseInt(reader.readLine().split(",")[column]);
    }

}
import java.io.*;
公共班机{
公共静态void main(字符串[]args)引发异常{
File File=新文件(“F:/Bignum/bignum1.csv”);
//获取总行数
int totalRows=getTotalRows(文件);
int列=4;
对于(int a=0;a
那么,你不想在内存中存储任何东西?下面的解决方案将直接从文件中读取值,但每次需要获取值时都会重新读取文件。警告:此工作负载(即读取文件)将以指数形式增大(?)行数越多

此解决方案不使用CSVReader,它只是使用逗号分隔符解析csv行。您可以轻松地修改此解决方案以使用CSVReader