0

我想返回所有的arraylist,但只有一个被返回,不明白为什么会发生这样的事情?花了一整天的时间试图理解这种错误的原因。

Code : -
  import java.io.FileReader;   
  import java.util.ArrayList; 
  import java.util.Arrays;
  import java.util.List;
  import au.com.bytecode.opencsv.CSVReader;


public class CSVFileReader {

   public List<String> main() {

    String startFile = "/Users/sample.csv";

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

        List<String> tempArr = null;

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

        String[] headers = reader.readNext();


        // 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);
                //System.out.println("the value of the array variable is :"+val);
                build.set(i, tempArr);
            }
        }

         for(List<String> string:build){

             System.out.println("The value of the array and their variables are :"+string);

         }
         for (int i=0; i<build.size();){
               System.out.println("Element :"+build.get(i));
               return build.get(i);
            }

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

public static void main(String[] args){
    CSVFileReader csvfilereader = new CSVFileReader();
    System.out.println("the returned variable is :"+csvfilereader.main());
    }
}

The CSV file is like:(Could Be A file of any size and any number of columns)
Keyword,AlternateKeyword,PrintValue
ego kit1,silicone baby dolls for sale,samsung
ego kit2,ego ce4,samsung
ego kit3,venus,samsung,
ego kit4,zz cream,samsung
ego kit5,samsung galaxy 7.7 case,samsung
ego kit6,apple,samsung

 And the Output is :

The value of the array and their variables are :[ego kit, ego kit, ego kit, ego kit, ego kit, ego kit]
The value of the array and their variables are :[silicone baby dolls for sale, ego ce4, venus, zz cream, samsung galaxy 7.7 case, apple]
The value of the array and their variables are :[Samsung, Samsung, Samsung, Samsung, Samsung, Samsung]
Element :[ego kit1, ego kit2, ego kit3, ego kit4, ego kit5, ego kit6]
the returned variable is :[ego kit, ego kit, ego kit, ego kit, ego kit, ego kit]

我想返回所有的 ArrayList。

此外,我想根据第一列元素的值将第一列的元素与第二列和第三列的元素按行组合在一起,以便进入 mysql 数据库。这里第一列是主 id,基于我们必须输入值,例如 ego kit1-->silicone dolls for sale、samsung 等。

我不想mysql入口的步骤只是想根据上面的元素创建Sql语句。

Java新手,卡在这一点上,请帮助。

4

1 回答 1

1
for (int i=0; i<build.size();){
               System.out.println("Element :"+build.get(i));
               return build.get(i);
}

for循环返回first element在 ArrayList 中找到的值build

我想返回所有的 ArrayList。

由于您想返回build列表中存在的所有元素(它们本身就是 ArrayLists),您需要更改return type您的main方法:

public List<List<String>> main(){

并按以下方式返回build列表:

for (int i=0; i<build.size();){
               System.out.println("Element :"+build.get(i));
               //return build.get(i);
}
return build;
于 2013-09-28T02:48:29.900 回答