如何在此处转换值:
List<String> values = new ArrayList<String>
至:
ArrayList<Custom>
编辑:
public class Custom {
public Custom Parse(String input) {
// What should I do here?
}
}
最佳答案
您可以使用:
List<Custom> customList = new ArrayList<Custom>();
for (String value: values) {
customList.add(new Custom(value));
}
尽管添加带有 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);
}
}
关于Java Android Arraylist字符串到ArrayList自定义类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12512226/