gpt4 book ai didi

java - 转换数组列表

转载 作者:太空宇宙 更新时间:2023-11-04 13:12:41 24 4
gpt4 key购买 nike

我想将对象的 arrayList 转换为特定对象类的 arrayList。你不能像转换普通对象一样这样做吗?

JsonParser jParse = CallApi.getUsers(session);
//This object contains users
ArrayList<Object> unknownObject = jParse.getList();
ArrayList<User> users = (ArrayList<User>)unknownObject;

最佳答案

这是我刚刚为类似任务创建的辅助方法。

public static <newType, oldType> ArrayList<newType> castArrayList(ArrayList<oldType> list){
ArrayList<newType> newlyCastedArrayList = new ArrayList<newType>();
for(oldType listObject : list){
newlyCastedArrayList.add((newType)listObject);
}
return newlyCastedArrayList;
}

可以这样使用

        ArrayList<DesiredObjectType> myArrayList = Util.castArrayList(
arrayListOfAnotherType
);

关于java - 转换数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33807723/

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