gpt4 book ai didi

java - 对象到 ArrayList 未经检查的转换

转载 作者:搜寻专家 更新时间:2023-10-31 19:47:54 24 4
gpt4 key购买 nike

用 -Xlint 编译我的代码我发现了这个:

warning: [unchecked] unchecked cast
found : java.lang.Object
required: java.util.ArrayList<java.lang.String>
ArrayList<String> msgData = (ArrayList<String>)element;

这是代码:

ArrayList<ArrayList<String>> msg = new ArrayList<ArrayList<String>>();
//add some data to msg
Iterator i = msg.iterator();
while(i.hasNext()) {

Object element = i.next();
ArrayList<String> msgData = (ArrayList<String>)element;
}

Actor 应该如何完成?

最佳答案

您的迭代器也需要一个类型,如下所示:

ArrayList<ArrayList<String>> msg = new ArrayList<ArrayList<String>>();
//add some data to msg
Iterator<ArrayList<String>> i = msg.iterator();
while(i.hasNext()) {
ArrayList<String> element = i.next();
}

附带说明一下,最好针对接口(interface)而不是实现进行编程。因此,你会写:

List<List<String>> msg = new ArrayList<List<String>>();
//add some data to msg
Iterator<List<String>> i = msg.iterator();
while(i.hasNext()) {
List<String> element = i.next();
}

关于java - 对象到 ArrayList<String> 未经检查的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13028563/

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