Java:List< MyClass>数组; [英] Java: Array of List<MyClass>

查看:64
本文介绍了Java:List< MyClass>数组;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个规范,要求我传递一组列表.数组的长度始终为2.我正在使用以下方法来完成此操作:

I have a spec that requires me to pass an array of lists. The array is always length 2. I am using the following to accomplish this:

List<MyClass> [] data = new ArrayList[2];
data[0] = new ArrayList<MyClass>();
data[1] = new ArrayList<MyClass>();

编译但发出警告:

uses unchecked or unsafe operations.

我知道Java中不允许使用泛型数组,但是我无法更改规范,并且上面的代码似乎运行良好.只要我意识到我从未将数组的元素重新分配为除类型ArrayList<MyClass>之外的其他内容,是否有任何理由我不应该仅仅抑制此警告并继续前进?

I understand that Arrays of generics are not allowed in Java however I cannot change the spec and the above code seems to work nicely. As long as I am conscious that I never reassign the elements of the array to be something other than type ArrayList<MyClass> are there any reasons I should not just suppress this warning and be on my way?

推荐答案

您所用的编译器会警告您,您的代码将不会对您将向数组添加哪种类型的值进行检查. 只要您确保仅ArrayList< MyClass>可以忽略此警告.类型添加到您的数组中.

The compiler in your case, warning you that your code isn't going to do any checking for you that which type of values you are adding to your array. You can ignore this warning, as long as you are ensuring that only ArrayList<MyClass> type are added to your array.

@SuppressWarnings("unchecked")出于某种原因出现在Java中,您可以取消显示警告,并让编译器知道您不需要进行类型检查.

@SuppressWarnings("unchecked") is present for a reason in java, you can suppress the warning and let your compiler know that you don't need it's type checking.

这篇关于Java:List&lt; MyClass&gt;数组;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆