【发布时间】:2015-03-04 12:53:20
【问题描述】:
我有一个基本的ArrayList<Car>,我想用它来制作一个String[]。很简单,但是我的 String 不知从何而来,我不明白这个问题。
这是我的功能。
private String[] makeListOfCars(){
Log.d("1st Car brand ", user.getCars().get(0).getBrand());
Log.d("2nd Car brand ", user.getCars().get(1).getBrand());
String result[] = new String[user.getCars().size()]; // Size > 0 because the user have at least one car here
for(int i = 0 ; i < user.getCars().size() ; i++){
result[i] += user.getCars().get(i).getBrand() + " " + user.getCars().get(i).getModel();
Log.d("Result ", result[i]);
}
return result;
}
这是我得到的输出:
03-04 13:49:32.470 27216-27216/com.example.bla.app D/1st Car brand﹕ 宝马
03-04 13:49:32.470 27216-27216/com.example.bla.app D/2nd Car 品牌:沃尔沃
03-04 13:49:32.470 27216-27216/com.example.bla.app D/结果:nullBmw 335i
03-04 13:49:32.470 27216-27216/com.example.bla.app D/结果:nullVolvo V40
有人可以解释一下这个 null 是从哪里来的吗?
【问题讨论】:
-
尝试:结果[i] = user.getCars().get(i).getBrand() + " " + user.getCars().get(i).getModel(); (而不是 +=)