0

I am having a resultList which is a string list were each element in this list is also a List<String>. Now I want to put my data into a csv sheet. For that I am using opencsv.

    List<String[]> data = new ArrayList<String[]>();
    for(int m = 0; m < resultList.get(0).size(); m++) {
        for (int i = 0; i < resultList.size(); i++) {

            data.add(new String[] {resultList.get(i).get(m).toString()});

        }
    }

    writer.writeAll(data);

    //close Writer
    writer.close();

My data should look like that:

enter image description here

However, my implementation gives me that:

enter image description here

In my implementation I am taking, knowing that every sublist has the same length, from each sublist the first element and add it to the array. Why am I getting this long row?

I appreciate your replies!

1
  • @Dukeling Can you recommend any code structure? Commented Feb 7, 2014 at 8:38

1 Answer 1

1

Because your loop is incorrect. You constructed a new String array in every inner loop, so every array has only on element. Try the following code:

List<String[]> data = new ArrayList<String[]>();
for(List<String> strlist : resultList) {

    String[] array = new String[strlist.size()];
    int offset = 0;
    for(String s : strlist) {
        array[offset ++] = s;
    }
    data.add(array);
}

============== Edited bellow ==================

I've re-ordered the loop to match your output requirement. Also, a sample input is provided to test the algorithm.

List<List<String>> resultList = new ArrayList<List<String>>();
for (int i = 1; i <= 9; i++) {

    List<String> innerList = new ArrayList<String>();
    resultList.add(innerList);
    for (int j = 1; j <= 9; j++) {
        innerList.add(j + "");
    }
}

List<String[]> data = new ArrayList<String[]>();

for(int m = 0; m < resultList.get(0).size(); m++) {

    String[] array = new String[resultList.size()];
    for (int i = 0; i < resultList.size(); i++) {
        array[i] = resultList.get(i).get(m).toString();
    }
    data.add(array);
}
for(String[] array : data) {
    System.out.println(Arrays.toString(array));
}
Sign up to request clarification or add additional context in comments.

4 Comments

@user2051347: I forgot data.add(array);, sorry for that. I've corrected my code.
Thx for your reply! However what I am currently getting is the resuls column wise and not row by row. Any suggestions how that can be fixed? I really would appreciate your reply!!!
@user2051347: Please give me a sample input of resultList, and expected output. I'll change the loop order to match your request.
Thx for your help! Sample input would be to have 3 string lists with the numbers 1 to 9 and then give them out that each row has the numbers 1 to 9(as it can be seen on the first picture). I really appreciate your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.