-1

for j=0我正在获取 arraylist,就像[ rahul 25] ...for j=1获取 arraylist 一样[rahu 25 arjun 30]. j=2 getting arraylist as[rahul 25 arjun 30 karthik 24]`。

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

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));    
4

3 回答 3

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 复制/粘贴代码。不要自己在这里输入,因为你往往会犯很多错别字和语法错误。

于 2013-10-15T07:01:20.507 回答
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);

    }   

}

于 2013-10-15T07:11:21.230 回答
0

您必须在循环name中添加字符串对象age

因为这些 String 对象被重载了。

并且还将条件更改为j < list.size()内部for循环

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);
}
于 2013-10-15T07:01:12.607 回答