Java:List< MyClass>数组;

编程入门 行业动态 更新时间:2024-10-28 04:15:39
本文介绍了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;数组;

本文发布于:2023-11-11 11:50:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1578356.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   amp   List   Java   gt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!