[英]Putting multiple String into an ArrayList
So I'm doing this: 所以我正在这样做:
int len = lv.getCount();
List<String> Cool = null;
SparseBooleanArray checked = lv.getCheckedItemPositions();
for (int i = 0; i < len; i++)
if (checked.get(i)) {
String item = String.valueOf(names.get(i));
int start = item.lastIndexOf('=') + 1;
int end = item.lastIndexOf('}');
String TEST = item.substring(start, end);
Log.d("Log", TEST);
Cool = new ArrayList<String>();
Cool.add(TEST);
}
String NEW = StringUtils.join(Cool, ',');
Log.d("Log", NEW);
Which evey time replaces the thing in the list with whatever the next item is. 哪个传送时间用下一个项目代替列表中的项目。 How do i make it put the strings after each other. 我如何使琴弦互相紧扣。
Thanks for the help. 谢谢您的帮助。
List<String> Cool = new ArrayList<String>();
create the list at the top 在顶部创建列表
Cool = new ArrayList<String>();
and delete this line because it will always create a new list what you dont want 并删除此行,因为它将始终创建一个新列表,使您不需要
You're constructing a new ArrayList
in every iteration of your for loop 您正在for循环的每次迭代中构造一个new ArrayList
Log.d("Log", TEST);
Cool = new ArrayList<String>(); // NOT HERE!!!!
Cool.add(TEST);
construct it once, outside the loop 在循环外构造一次
List<String> Cool = new ArrayList<String>(); // also Cool should be cool.
the reason it keeps resetting the list is because you initialized the List in a loop. 之所以不断重置列表,是因为您在循环中初始化了列表。 Initialize it outside the loop and the algorithm will work. 在循环外对其进行初始化,该算法将起作用。
Initialization: 初始化:
Cool = new ArrayList(); 酷= new ArrayList();
Corrected code: 更正的代码:
int len = lv.getCount();
List<String> Cool = new ArrayList<String>();
SparseBooleanArray checked = lv.getCheckedItemPositions();
for (int i = 0; i < len; i++)
if (checked.get(i)) {
String item = String.valueOf(names.get(i));
int start = item.lastIndexOf('=') + 1;
int end = item.lastIndexOf('}');
String TEST = item.substring(start, end);
Log.d("Log", TEST);
Cool.add(TEST);
}
String NEW = StringUtils.join(Cool, ',');
Log.d("Log", NEW);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.