0

I am having an array were I add several lists to this array. After that I would like to add other columns to this array which do not have the same length.

int minSize = Math.min(testList.get(j).getDate().size(), testList.get(j).getTotalReturnIndex().size());

List<String[]> data = new ArrayList<String[]>();
for(int m = 0; m < minSize; m++)
{
    //TODO remove .replace('.', ',')
    data.add(new String[] {testList.get(j).getCompanyName(), testList.get(j).getDate().get(m), testList.get(j).getCurrency(), testList.get(j).getTotalReturnIndex().get(m).toString().replace('.', ','), testList.get(j).getPrice().get(m).toString().replace('.', ','), "", sublist.get(m)});
    //here I am getting an `java.lang.IndexOutOfBoundsException:` because the list is not so long like the others
}

This is how I add the first part(the first 5 columns of the picture) to the array.

Visualized in excel my array should look like that in the end:

enter image description here

How to add the other 3 columns to my array beginning with sublist based on the above code?

I really appreciate your answer!

0

1 Answer 1

1

You could try it with a simple ternary:

int minSize = Math.min(testList.get(j).getDate().size(), testList.get(j).getTotalReturnIndex().size());

List<String[]> data = new ArrayList<String[]>();
for(int m = 0; m < minSize; m++) {
    //TODO remove .replace('.', ',')
    data.add(new String[] {testList.get(j).getCompanyName(),
        testList.get(j).getDate().get(m), 
        testList.get(j).getCurrency(), 
        testList.get(j).getTotalReturnIndex().get(m).toString().replace('.', ','),
        testList.get(j).getPrice().get(m).toString().replace('.', ','),
        "", 
        (m < sublist.size())? sublist.get(m) : "" 
    });
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thx for your answer! What is better if I only have one value: new String[]{""} or "" ?
it should only be "", because you only can put simple Strings in a String Array. Sorry, I was confused, because there are 3 more columns in your excel sheet. I will edit the code.

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.