简体   繁体   English

Java-使列表大小动态

[英]Java - Make List Size Dynamic

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. countOptions返回一个整数,一个人可以是10,另一个可以是3。 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. 这显然不起作用,因为当我需要每个"0"具有自己的索引时,它将ret设置为单个索引"0","0","0","0" (如果countOptions为4)的列表。

I hope this all makes sense, and I really hope it is possible. 我希望这一切都是有道理的,而且我真的希望有可能。

Try to use ArrayList instead of String concatenation. 尝试使用ArrayList而不是String串联。

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

for(int i = 0; i < countOptions; i++){
    responseListSize.add( addResponseTuple );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM