Arraylist :使用未经检查或不安全的操作

Arraylist : uses unchecked or unsafe operations

我的应用程序中有两个警告阻止我 "uses unchecked or unsafe operations"。我试图理解其他帖子,但我根本不明白发生了什么,所以我仍处于起点...

我的适配器中有一个 class(“(ArrayList) results.values”中的警告):

arrayList = (ArrayList<Sound>) results.values;

另一个在我的 Activity 中,我正在使用数组列表(警告在 "Collections.sort" 上):

        Collections.sort(names);

你能解释一下这是什么意思以及如何解决吗?

感谢您的帮助。

results.values 的类型显然是 Object,因此将其转换为 ArrayList<Sound> 是危险的,因为它的 运行 时间类型可能与该类型不兼容。如果 result.values 始终是 ArrayList<Sound> 并且您可以更改其类型,则只需将其更改为 ArrayList<Sound>。如果您绝对没有能力更改类型并且您绝对确定转换在 运行 时间有效,那么您可以在该行上方添加 @SuppressWarnings("unchecked") 以抑制警告。

我不知道您的摘录中 names 的类型是什么,但它的 compareTo() 方法可能具有类似的不安全转换,因为未指定参数类型。查找类似于 this question.

的问题