For example:
例如:
javac Foo.java
Note: Foo.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
7 个解决方案
#1
296
This comes up in Java 5 and later if you're using collections without type specifiers (e.g., Arraylist()
instead of ArrayList<String>()
). It means that the compiler can't check that you're using the collection in a type-safe way, using generics.
这在Java 5中出现,如果您使用的是没有类型说明符的集合(例如Arraylist(),而不是Arraylist
To get rid of the warning, just be specific about what type of objects you're storing in the collection. So, instead of
为了消除这个警告,只需要知道您在集合中存储的对象的类型。因此,而不是
List myList = new ArrayList();
use
使用
List<String> myList = new ArrayList<String>();
In Java 7 you can shorten generic instantiation by using Type Inference.
在Java 7中,您可以使用类型推断来缩短通用实例化。
List<String> myList = new ArrayList<>();
#2
155
If you do what it suggests and recompile with the "-Xlint:unchecked" switch, it will give you more detailed information.
如果你用“-Xlint:不检查”开关来做它建议和重新编译的东西,它会给你更详细的信息。
As well as the use of raw types (as described by the other answers), an unchecked cast can also cause the warning.
除了使用原始类型(如其他答案所述),未检查的类型也会引起警告。
Once you've compiled with -Xlint, you should be able to rework your code to avoid the warning. This is not always possible, particularly if you are integrating with legacy code that cannot be changed. In this situation, you may decide to suppress the warning in places where you know that the code is correct:
使用-Xlint编译后,您应该能够重新编写代码以避免警告。这并不总是可能的,特别是如果您正在与无法更改的遗留代码进行集成。在这种情况下,您可能决定在您知道代码正确的地方禁止警告:
@SuppressWarnings("unchecked")
public void myMethod()
{
//...
}
#3
11
This warning means that your code operates on a raw type, recompile the example with the
这个警告意味着您的代码在一个原始类型上运行,重新编译这个示例。
-Xlint:unchecked
to get the details
去了解更多的细节
like this:
是这样的:
javac YourFile.java -Xlint:unchecked
Main.java:7: warning: [unchecked] unchecked cast
clone.mylist = (ArrayList<String>)this.mylist.clone();
^
required: ArrayList<String>
found: Object
1 warning
docs.oracle.com talks about it here: http://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html
oracle.com在这里谈论它:http://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html。
#4
5
for example when you call a function that returns Generic Collections and you don't specify the generic parameters yourself.
例如,当您调用一个返回泛型集合的函数时,您不需要自己指定泛型参数。
for a function
为一个函数
List<String> getNames()
List names = obj.getNames();
will generate this error.
将会产生这个错误。
To solve it you would just add the parameters
为了解决这个问题,你只需要添加参数。
List<String> names = obj.getNames();
#5
5
The "unchecked or unsafe operations" warning was added when java added Generics, if I remember correctly. It's usually asking you to be more explicit about types, in one way or another.
如果我没记错的话,当java添加泛型时,会添加“未检查或不安全的操作”警告。它通常要求你以这样或那样的方式,更加明确地描述类型。
For example. the code ArrayList foo = new ArrayList();
triggers that warning because javac is looking for ArrayList<String> foo = new ArrayList<String>();
为例。代码ArrayList foo = new ArrayList();触发这个警告,因为javac正在查找ArrayList
#6
1
I just want to add one example of the kind of unchecked warning I see quite often. If you use classes that implement an interface like Serializable, often you will call methods that return objects of the interface, and not the actual class. If the class being returned must be cast to a type based on generics, you can get this warning.
我只是想添加一个我经常看到的未经检查的警告的例子。如果您使用的类实现了像Serializable这样的接口,通常会调用返回接口对象的方法,而不是实际的类。如果返回的类必须被转换为基于泛型的类型,您可以得到这个警告。
Here is a brief (and somewhat silly) example to demonstrate:
这里有一个简短的(有点愚蠢的)例子来说明:
import java.io.Serializable;
public class SimpleGenericClass<T> implements Serializable {
public Serializable getInstance() {
return this;
}
// @SuppressWarnings("unchecked")
public static void main() {
SimpleGenericClass<String> original = new SimpleGenericClass<String>();
// java: unchecked cast
// required: SimpleGenericClass<java.lang.String>
// found: java.io.Serializable
SimpleGenericClass<String> returned =
(SimpleGenericClass<String>) original.getInstance();
}
}
getInstance() returns an object that implements Serializable. This must be cast to the actual type, but this is an unchecked cast.
getInstance()返回一个实现Serializable的对象。这必须转换为实际类型,但这是一个未检查的类型。
#7
0
The solution would be to use specific type in <> like ArrayList.
解决方案是在<> like ArrayList中使用特定类型。
example:
例子:
File curfolder = new File( "C:\\Users\\username\\Desktop"); File[] file = curfolder.listFiles(); ArrayList filename = Arrays.asList(file);
文件curfolder =新文件(“C:\\ \\ \用户名\\桌面”);[]文件= curfolder.listFiles();ArrayList文件名= arrays . aslist(文件);
above code generate warning because ArrayList is not of specific type.
上面的代码生成警告,因为ArrayList不是特定类型的。
File curfolder = new File( "C:\\Users\\username\\Desktop"); File[] file = curfolder.listFiles(); ArrayList<File> filename = Arrays.asList(file);
文件curfolder =新文件(“C:\\ \\ \用户名\\桌面”);[]文件= curfolder.listFiles();ArrayList <文件> 文件名= arrays . aslist(文件);
above code will do fine. Only change is in third line after ArrayList.
以上代码行。只有在ArrayList之后的第三行才会发生更改。
#1
296
This comes up in Java 5 and later if you're using collections without type specifiers (e.g., Arraylist()
instead of ArrayList<String>()
). It means that the compiler can't check that you're using the collection in a type-safe way, using generics.
这在Java 5中出现,如果您使用的是没有类型说明符的集合(例如Arraylist(),而不是Arraylist
To get rid of the warning, just be specific about what type of objects you're storing in the collection. So, instead of
为了消除这个警告,只需要知道您在集合中存储的对象的类型。因此,而不是
List myList = new ArrayList();
use
使用
List<String> myList = new ArrayList<String>();
In Java 7 you can shorten generic instantiation by using Type Inference.
在Java 7中,您可以使用类型推断来缩短通用实例化。
List<String> myList = new ArrayList<>();
#2
155
If you do what it suggests and recompile with the "-Xlint:unchecked" switch, it will give you more detailed information.
如果你用“-Xlint:不检查”开关来做它建议和重新编译的东西,它会给你更详细的信息。
As well as the use of raw types (as described by the other answers), an unchecked cast can also cause the warning.
除了使用原始类型(如其他答案所述),未检查的类型也会引起警告。
Once you've compiled with -Xlint, you should be able to rework your code to avoid the warning. This is not always possible, particularly if you are integrating with legacy code that cannot be changed. In this situation, you may decide to suppress the warning in places where you know that the code is correct:
使用-Xlint编译后,您应该能够重新编写代码以避免警告。这并不总是可能的,特别是如果您正在与无法更改的遗留代码进行集成。在这种情况下,您可能决定在您知道代码正确的地方禁止警告:
@SuppressWarnings("unchecked")
public void myMethod()
{
//...
}
#3
11
This warning means that your code operates on a raw type, recompile the example with the
这个警告意味着您的代码在一个原始类型上运行,重新编译这个示例。
-Xlint:unchecked
to get the details
去了解更多的细节
like this:
是这样的:
javac YourFile.java -Xlint:unchecked
Main.java:7: warning: [unchecked] unchecked cast
clone.mylist = (ArrayList<String>)this.mylist.clone();
^
required: ArrayList<String>
found: Object
1 warning
docs.oracle.com talks about it here: http://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html
oracle.com在这里谈论它:http://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html。
#4
5
for example when you call a function that returns Generic Collections and you don't specify the generic parameters yourself.
例如,当您调用一个返回泛型集合的函数时,您不需要自己指定泛型参数。
for a function
为一个函数
List<String> getNames()
List names = obj.getNames();
will generate this error.
将会产生这个错误。
To solve it you would just add the parameters
为了解决这个问题,你只需要添加参数。
List<String> names = obj.getNames();
#5
5
The "unchecked or unsafe operations" warning was added when java added Generics, if I remember correctly. It's usually asking you to be more explicit about types, in one way or another.
如果我没记错的话,当java添加泛型时,会添加“未检查或不安全的操作”警告。它通常要求你以这样或那样的方式,更加明确地描述类型。
For example. the code ArrayList foo = new ArrayList();
triggers that warning because javac is looking for ArrayList<String> foo = new ArrayList<String>();
为例。代码ArrayList foo = new ArrayList();触发这个警告,因为javac正在查找ArrayList
#6
1
I just want to add one example of the kind of unchecked warning I see quite often. If you use classes that implement an interface like Serializable, often you will call methods that return objects of the interface, and not the actual class. If the class being returned must be cast to a type based on generics, you can get this warning.
我只是想添加一个我经常看到的未经检查的警告的例子。如果您使用的类实现了像Serializable这样的接口,通常会调用返回接口对象的方法,而不是实际的类。如果返回的类必须被转换为基于泛型的类型,您可以得到这个警告。
Here is a brief (and somewhat silly) example to demonstrate:
这里有一个简短的(有点愚蠢的)例子来说明:
import java.io.Serializable;
public class SimpleGenericClass<T> implements Serializable {
public Serializable getInstance() {
return this;
}
// @SuppressWarnings("unchecked")
public static void main() {
SimpleGenericClass<String> original = new SimpleGenericClass<String>();
// java: unchecked cast
// required: SimpleGenericClass<java.lang.String>
// found: java.io.Serializable
SimpleGenericClass<String> returned =
(SimpleGenericClass<String>) original.getInstance();
}
}
getInstance() returns an object that implements Serializable. This must be cast to the actual type, but this is an unchecked cast.
getInstance()返回一个实现Serializable的对象。这必须转换为实际类型,但这是一个未检查的类型。
#7
0
The solution would be to use specific type in <> like ArrayList.
解决方案是在<> like ArrayList中使用特定类型。
example:
例子:
File curfolder = new File( "C:\\Users\\username\\Desktop"); File[] file = curfolder.listFiles(); ArrayList filename = Arrays.asList(file);
文件curfolder =新文件(“C:\\ \\ \用户名\\桌面”);[]文件= curfolder.listFiles();ArrayList文件名= arrays . aslist(文件);
above code generate warning because ArrayList is not of specific type.
上面的代码生成警告,因为ArrayList不是特定类型的。
File curfolder = new File( "C:\\Users\\username\\Desktop"); File[] file = curfolder.listFiles(); ArrayList<File> filename = Arrays.asList(file);
文件curfolder =新文件(“C:\\ \\ \用户名\\桌面”);[]文件= curfolder.listFiles();ArrayList <文件> 文件名= arrays . aslist(文件);
above code will do fine. Only change is in third line after ArrayList.
以上代码行。只有在ArrayList之后的第三行才会发生更改。