java Arraylist 的未检查警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5142906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unchecked warnings for Arraylist
提问by foosion
Why am I getting these 4 warnings from -Xlint and what should I do about them? I'm just starting in Java, so am likely missing something obvious.
为什么我会从 -Xlint 收到这 4 个警告,我该怎么办?我刚刚开始使用 Java,所以可能会遗漏一些明显的东西。
import java.util.*;
class CompareGerbils implements Comparator {
public int compare(Object o1, Object o2) {
return ((Gerbil)o2).number() - ((Gerbil)o1).number();
}
}
class Gerbil {
int gerbilNumber;
Gerbil(int gN) {
gerbilNumber = gN;
}
int number() {
return gerbilNumber;
}
}
public class lt {
public static void main(String[] args) {
// I'd like to be able to add both ints and strings to list
ArrayList list = new ArrayList();
//unchecked call warning:
list.add(1);
//unchecked call warning:
list.add("b");
ArrayList<Gerbil> gerbillist = new ArrayList<Gerbil>();
for(int i = 0; i < 5; i++) {
gerbillist.add(new Gerbil(i));
}
//unchecked conversion warning
//unchecked method invocation
Collections.sort(gerbillist, new CompareGerbils());
}
}
EDIT: replies so far have answered the Arraylist declaration. How about the sort warnings at the bottom of the code? thanks
编辑:到目前为止的答复已经回答了 Arraylist 声明。代码底部的排序警告怎么样?谢谢
回答by Lucas Zamboulis
You're getting this because you have not defined a data type for the ArrayList list
. The only way to add both Strings and Integers in list
without getting warnings is by defining it as ArrayList<Object> list
- which is what happens here implicitly (line list.add(1);
is implicitly converting 1
to new Integer(1)
- this is called autoboxing). Also note that if you want to have both Strings and Integers in lists
, the sorting method does not really make sense - how are you expecting things to get sorted, alphabetically or numerically?
你得到这个是因为你没有为ArrayList list
. 在list
没有警告的情况下添加字符串和整数的唯一方法是将其定义为ArrayList<Object> list
- 这就是这里隐式发生的事情(行list.add(1);
隐式转换1
为new Integer(1)
- 这称为自动装箱)。另请注意,如果您想在 中同时包含字符串和整数lists
,则排序方法实际上没有意义-您希望如何按字母顺序或数字顺序对事物进行排序?
Edit: Also, it is not considered good practice to declare a concrete type (i.e. ArrayList<Object> list
) unless you have very good reasons to do so. It is recommended that you initialise using an interface, i.e. List<Object> list
.
编辑:此外,ArrayList<Object> list
除非您有充分的理由这样做,否则声明具体类型(即)不被认为是好的做法。建议您使用接口进行初始化,即List<Object> list
.
So, your code would have to be like this (note the part Comparator<Gerbil>
which fixes the warning in Collections.sort
):
因此,您的代码必须是这样的(注意Comparator<Gerbil>
修复 中警告的部分Collections.sort
):
// I'd like to be able to add both ints and strings to list
List<Object> list = new ArrayList<Object>();
list.add(new Integer(1));
list.add(new String("b"));
List<Gerbil> gerbillist = new ArrayList<Gerbil>();
for(int i = 0; i < 5; i++) {
gerbillist.add(new Gerbil(i));
}
Collections.sort(gerbillist, new Comparator<Gerbil>() {
public int compare(Gerbil o1, Gerbil o2) {
int diff = o1.getNumber() - o2.getNumber();
if (diff > 0)
return 1;
else if (diff <0)
return -1;
else
return 0;
}
});
With respect to the Gerbil class, I suggest you use the form getNumber
as a method name rather than number
- it's a de facto standard for method names to retrieve the value of a member variable (and, respectively setNumber(int value)
for setting it):
关于 Gerbil 类,我建议您使用表单getNumber
作为方法名称,而不是number
- 它是方法名称的事实上的标准,用于检索成员变量的值(以及分别setNumber(int value)
用于设置它):
class Gerbil {
int gerbilNumber;
Gerbil(int gN) {
gerbilNumber = gN;
}
int getNumber() {
return gerbilNumber;
}
}
回答by Peter Lawrey
The warning occurs when you are using a non-generic type in a context where a generic is expected. The compile is saying, you might be right, but I cannot check the type for you.
当您在需要泛型的上下文中使用非泛型类型时,会出现警告。编译说,你可能是对的,但我无法为你检查类型。
You cna either;
你也可以;
- Make the type the correct generic.
- Turn off the warning with @SuppressWarnings
- Ignore the warning.
- 使类型成为正确的泛型。
- 使用@SuppressWarnings 关闭警告
- 忽略警告。
EDIT: In this example, you have to pick a super class/interface of the elements in the list.
编辑:在本例中,您必须选择列表中元素的超类/接口。
// I'd like to be able to add both ints and strings to list
List<Object> list = new ArrayList<Object>();
list.add(1);
list.add("b");
Instead of Object you could pick Seralizable
or Comparable
however eneither is likely to be useful. Indeed a List<Object>
is rarely useful except in exercises.
您可以选择而不是 ObjectSeralizable
或者Comparable
两者都可能有用。List<Object>
除了练习之外,a确实很少有用。
The correct way to implement the Comparator
is to use comparison. Using -
is only valid if you know this cannot possibly overflow. e.g. 2000000000 - -2000000000 < 0 whereas you might expect 2000000000 - -2000000000 > 0
实现 的正确方法Comparator
是使用比较。-
仅当您知道这不可能溢出时使用才有效。例如 2000000000 - -2000000000 < 0 而你可能期望 2000000000 - -2000000000 > 0
class CompareGerbils implements Comparator<Gerbil> {
public int compare(Gerbil a, Gerbil b) {
return a.number() > b.number() ? +1
: a.number() < b.number() ? -1 : 0;
}
}
For testing purposes I suggest trying to sort a List which is not already sorted. A simple way to do this is to use the shuffle() method. This could still be sorted but the large the list, the less likely that is the case.
出于测试目的,我建议尝试对尚未排序的 List 进行排序。一个简单的方法是使用 shuffle() 方法。这仍然可以排序,但列表越大,这种情况的可能性就越小。
Collections.shuffle(gerbillis);
回答by Paul McKenzie
public static void main(String[] args) {
final List<Object> list = new ArrayList<Object>();
list.add(1);
list.add("b");
final List<Gerbil> gerbillist = new ArrayList<Gerbil>();
for (int i = 0; i < 5; i++) {
gerbillist.add(new Gerbil(i));
}
Collections.sort(gerbillist, new CompareGerbils());
}
回答by Dead Programmer
you need to know Generics http://download.oracle.com/javase/1.5.0/docs/guide/language/generics.html
you are trying to add
primitive
and a string inarraylist
. either u do this to take anything
你需要知道泛型http://download.oracle.com/javase/1.5.0/docs/guide/language/generics.html
您正在尝试
primitive
在arraylist
. 要么你这样做是为了拿任何东西
List<Object> list = new ArrayList<Object>();
List<Object> list = new ArrayList<Object>();
or if you want to allow only strings
或者如果你只想允许字符串
List<String> list = new ArrayList<String>();