1

我想StringArrayList. 目前,我只能从ArrayList. 我的代码:

eachstep = new ArrayList<String>();
for (int i = 0; i < parsedsteps.size(); i++) {
eachstep.add(parsedsteps.get(i).replaceAll("<[^>]*>", ""));
}                   
for (int i = 0; i < eachstep.size(); i++) {
    String directions = i + "."+" "+eachstep.get(i)+"\n"+;
} 

给我:

3.  This is step 3.

代替:

1. This is step 1.          
2. This is step 2.
3. This is step 3.

如何使我的 for 循环String使用来自的所有值创建一个ArrayList

4

3 回答 3

2

您需要在循环之外声明您的字符串,我建议也使用StringBuilder,这样构建字符串更有效。

StringBuilder directions = new StringBuilder();
for( int i = 0; i < eachstep.size(); i++ )
{
    directions.append( i + "." + " " + eachstep.get( i ) + "\n" );
}

然后,当您想从 StringBuilder 中取出字符串时,只需调用directions.toString().

于 2012-10-20T18:36:14.543 回答
0
String directions = "";
for (int i = 0; i < eachstep.size(); i++) {
    directions += i + "."+" "+eachstep.get(i)+"\n";
} 
于 2012-10-20T18:35:33.813 回答
0

试试这个

   eachstep = new ArrayList<String>();
   for (int i = 0; i < parsedsteps.size(); i++) {
       eachstep.add(parsedsteps.get(i).replaceAll("<[^>]*>", ""));
   }
   String directions="";
   for (int i = 0; i < eachstep.size(); i++) {
       directions += i + "."+" "+eachstep.get(i)+"\n"+;
   } 

如果您有大尺寸的字符串数组,您可能需要考虑使用 StringBuilder,例如

  StringBuilder builder = new StringBuilder();
  for(String str: eachstep ){
       builder.append(i).append(".").append(str).append("\n");
  }
  String direction = builder.toString();
于 2012-10-20T18:40:56.353 回答