gpt4 book ai didi

Java Android Arraylist字符串转ArrayList自定义类

转载 作者:行者123 更新时间:2023-11-30 07:22:41 25 4
gpt4 key购买 nike

如何在此处转换:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com