0

I need to make the size of a list fixed dynamic but dynamic based on the parameters of its initiation.

I am trying the following -

int countOptions = countOptions(s_sqlContext, s_organisationId);
String responseListSize = "\"0\"";
String addResponseTuple = ",\"0\"";

for (int i = 1; i < countOptions; i++) {
    responseListSize = responseListSize.concat(addResponseTuple);
}

List<String> ret = Arrays.asList(new String[] { responseListSize });

countOptions returns an integer that may be 10 for one person or 3 for another. This obviously does not work because it is setting ret as a single index List of "0","0","0","0" (if countOptions is 4), when I need each "0" to have its own index.

I hope this all makes sense, and I really hope it is possible.

9
  • Would it not make more sense to put length constraints around an ArrayList instead? Commented Apr 28, 2014 at 10:28
  • @ydaetskcoR Would this be something as simple as .size(countOptions) - am I really being this stupid? - monday morning - I am a learner as you may have gathered, only graduated last academic [= Commented Apr 28, 2014 at 10:31
  • ^^ idiocy justification Commented Apr 28, 2014 at 10:31
  • @ydaetskcoR so size() does not wok with a List<String> any ideas? Commented Apr 28, 2014 at 10:38
  • 1
    @Phish Yes which is why I would not use Arrays.asList() at all in this situation, I would do exactly what is being done in the accepted answer, add directly to a new list in your loop, if you actually need the concatenation of zeros for something else, build that later, from the list. Commented Apr 28, 2014 at 11:21

1 Answer 1

2

Try to use ArrayList instead of String concatenation.

List<String> responseListSize = new ArrayList<String>(countOptions);

for(int i = 0; i < countOptions; i++){
    responseListSize.add( addResponseTuple );
}
Sign up to request clarification or add additional context in comments.

Comments