[英]Java Android Arraylist string to ArrayList custom class
How can I convert values here: 我怎样才能在这里转换值 :
List<String> values = new ArrayList<String>
to : 至 :
ArrayList<Custom>
EDIT : 编辑 :
public class Custom {
public Custom Parse(String input) {
// What should I do here?
}
}
You could use: 你可以使用:
List<Custom> customList = new ArrayList<Custom>();
for (String value: values) {
customList.add(new Custom(value));
}
Although it would be better just to add a constructor with a String
argument: 虽然最好只添加一个带String
参数的构造函数:
class Custom {
private final String input;
public Custom(String input) {
this.input = input;
}
// not needed but implemented for completeness
public static Custom parse(String input) {
return new Custom(input);
}
}
Assuming your list of Custom
objects has the same size with values
list. 假设您的Custom
对象列表与values
列表具有相同的大小。 With one enhanced for-loop, set the appropriate fields of your objects like this: 使用一个增强的for循环,设置对象的相应字段,如下所示:
int i=0;
for(String str:values)
customList.get(i++).setSomeProperty(str);
您可以在此线程上使用Google Collections库找到解决方案将List <String>转换为List <Integer>(或任何扩展Number的类)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.