Arraylist值成为字符串

时间:2013-10-15 06:56:14

标签: java arraylist

for j=0正在获得像[ rahul 25] ...for j=1这样的arraylist,因为j = 2 [rahu 25 arjun 30]. [rahul 25 arjun 30 karthik 24]将getting arraylist as作为for j= 2。“

我正在将这些数组列表转换为字符串。

[rahul rahul rahul ]

我的结果是名称:: [25 25 25]                      年龄:: [rahul arjun karthik]

结果必须像名字:: [25 30 24].                       年龄:: private void data(ArrayList<person> list) { ArrayList<String> nameValue = new ArrayList<String>(); ArrayList<String> ageValue = new ArrayList<String>(); String name,age=null; int j=0; String data=null; String[] parts = null; for ( j = 0; j <= list.size(); j++) { data=list.toString(); System.out.println("arraylist :::::"+data); parts = da.split(" "); name = parts[0]; age = parts[1]; } nameVlaue.add(name); ageValue.add(age); System.out.println("name::"+ nameVlaue.add(name));// System.out.println("age ::"+ ageValue.add(age));

{{1}}

3 个答案:

答案 0 :(得分:2)

您需要进行以下更改。

for ( j = 0; j < list.size(); j++) // j<list.size(), equals will give an IOOBE
{                   
  data=list.get(j).toString(); // Get the next object always, not the list as a whole
  // I hope the toString() method of your Person class gives the output as space delimited
  System.out.println("arraylist :::::"+data);
  parts = data.split(" "); // Split the data, not da
  name = parts[0]; 
  age = parts[1]; 
  nameVlaue.add(name); // This should be in the loop as you need to add every new value to the list
  ageValue.add(age); // This should be in the loop as you need to add every new value to the list
}

作为旁注,请始终从IDE复制/粘贴代码。不要自己在这里输入,因为你往往会犯很多拼写错误和语法错误。

答案 1 :(得分:0)

您必须在循环中添加String对象nameage

导致这些String对象重载。

并且还将条件更改为for-loop中的j < list.size()

for ( j = 0; j < list.size(); j++) 
{                   

  data=list.toString(); 
  System.out.println("arraylist :::::"+data);
  parts = da.split(" ");
  name = parts[0]; 
  age = parts[1]; 
  nameVlaue.add(name);
  ageValue.add(age);
}

答案 2 :(得分:0)

使用此代码工作正常: public static void main(String [] args){

            // TODO Auto-generated method stub
        ArrayList<String> list = new ArrayList<String>();
        list.add("rahul 25 ");
        list.add("karthik 24");
        list.add("arjun 30");

          ArrayList<String> nameValue = new ArrayList<String>();        
          ArrayList<String>  ageValue = new ArrayList<String>();        

          String name="",age="";

          int j=0;              
          String data=null; 
          String[] parts = null;





          data=list.toString(); 
          System.out.println("arraylist :::::"+data);
         /* parts = data.split(",");
          for(String str:parts){
             String namePart[]=str.split(" ");
             name=name+" "+namePart[0];
             age=age+" "+namePart[1];
          }*/
          for (String str:list) 
          {   
             String namePart[]=str.split(" ");
            name = name+" "+namePart[0]; 
            age = age+" "+namePart[1]; 
          }
         /* name = parts[0]; 
          age = parts[1]; 
        */

        nameValue.add(name);
       ageValue.add(age);

System.out.println("name::"+ nameValue);//                
System.out.println("age ::"+ ageValue);

    }   

}