0

私のプロジェクトでは、値を文字列に動的に格納する必要があり、その文字列を「,」で分割する必要があります。どうやってやるの ?私を助けてください..

私のコード:

static ArrayList<ArrayList<String>> listhere;
ArrayList<String> arropids;
String arropids1; 


    for(int q=0;q<listhere.size();q++)
                {
                  arropids = listhere.get(q);

                  if(arropids.get(3).equals("1"))
                  {
                      arropids1 += arropids.get(0) + ","; 


                  System.out.println("arropids1"+arropids1);

                }
                } 
4

2 に答える 2

2

文字列を初期化していないため、NullPointerExceptionが発生している必要があります。

String arropids1="";

問題は解決しますが、このタスクには文字列をお勧めしません。文字列は不変タイプであるため、この目的でStringBufferを使用できるため、次のコードをお勧めします。

static ArrayList<ArrayList<String>> listhere;
ArrayList<String> arropids;

StringBuffer buffer=new StringBuffer();

    for(int q=0;q<listhere.size();q++)
                {
                  arropids = listhere.get(q);

                  if(arropids.get(3).equals("1"))
                  {
                      buffer.append(arropids.get(0));
                      buffer.append(","); 


                  System.out.println("arropids1"+arropids1);

                }
                }

最後に、次の方法でそのバッファから文字列を取得します。

 String arropids1=buffer.toString(); 
于 2012-04-14T05:01:19.417 に答える
0

解析をforループに格納した後に結果を分割するには、格納された文字列でsplitメソッドを使用し、次のように文字列配列と等しくなるように設定します。

static ArrayList<ArrayList<String>> listhere;
ArrayList<String> arropids;
String arropids1 = ""; 


for(int q=0;q<listhere.size();q++) {
              arropids = listhere.get(q);

              if(arropids.get(3).equals("1"))
              {
                  arropids1 += arropids.get(0) + ","; 


              System.out.println("arropids1"+arropids1);

              }
      }
      String[] results = arropids1.split(",");
      for (int i =0; i < results.length; i++) {
           System.out.println(results[i]);
      }

これがあなたが探しているものであることを願っています。

于 2012-04-14T05:07:47.960 に答える