62,615
社区成员
发帖
与我相关
我的任务
分享
警告: [unchecked] 未经检查的转换
Map<String, String> urls = (HashMap<String, String>) chunk.body;
需要: HashMap<String,String>
找到: Object
public class A<T>
{
private T obj;
public void setObj(T obj) {
this.obj = obj;
}
public T getObj() {
return obj;
}
}
泛型是需要你指定某种类型的,如果你要A能传递任何对象就要指定泛型为Object,例如:A<Object> a = new A<Object>(); 这样你就能传递任何对象而没有警告了
import java.util.HashMap;
public class NewMainTest<T> {
private T obj;
public void setObj(T obj) {
this.obj = obj;
}
public T getObj() {
return obj;
}
/**
* @param jsonArray the command line arguments
*/
public static void main(String args[]) throws Exception {
NewMainTest nmt = new NewMainTest<>();
HashMap<String, String> map = new HashMap<>();
nmt.setObj(map);
HashMap<String, String> map1 = (HashMap<String, String>) nmt.getObj();
//map1.get;
}
}
警告啊。[/quote]
那就只能加注解了 [/quote]怎么加注解?[/quote]
@SuppressWarnings("unchecked")可以加在一行代码的前面,只对一行起作用啊,好用![/quote]
嗯 看SuppressWarnings目标范围 @Target(value={TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE})
可以加在除了package和annotation的任何东西前面[/quote]多谢了!
import java.util.HashMap;
public class NewMainTest<T> {
private T obj;
public void setObj(T obj) {
this.obj = obj;
}
public T getObj() {
return obj;
}
/**
* @param jsonArray the command line arguments
*/
public static void main(String args[]) throws Exception {
NewMainTest nmt = new NewMainTest<>();
HashMap<String, String> map = new HashMap<>();
nmt.setObj(map);
HashMap<String, String> map1 = (HashMap<String, String>) nmt.getObj();
//map1.get;
}
}
警告啊。[/quote]
那就只能加注解了 [/quote]怎么加注解?[/quote]
@SuppressWarnings("unchecked")可以加在一行代码的前面,只对一行起作用啊,好用![/quote]
嗯 看SuppressWarnings目标范围 @Target(value={TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE})
可以加在除了package和annotation的任何东西前面
import java.util.HashMap;
public class NewMainTest<T> {
private T obj;
public void setObj(T obj) {
this.obj = obj;
}
public T getObj() {
return obj;
}
/**
* @param jsonArray the command line arguments
*/
public static void main(String args[]) throws Exception {
NewMainTest nmt = new NewMainTest<>();
HashMap<String, String> map = new HashMap<>();
nmt.setObj(map);
HashMap<String, String> map1 = (HashMap<String, String>) nmt.getObj();
//map1.get;
}
}
警告啊。[/quote]
那就只能加注解了 [/quote]怎么加注解?[/quote]
@SuppressWarnings("unchecked")可以加在一行代码的前面,只对一行起作用啊,好用!
import java.util.HashMap;
public class NewMainTest<T> {
private T obj;
public void setObj(T obj) {
this.obj = obj;
}
public T getObj() {
return obj;
}
/**
* @param jsonArray the command line arguments
*/
public static void main(String args[]) throws Exception {
NewMainTest nmt = new NewMainTest<>();
HashMap<String, String> map = new HashMap<>();
nmt.setObj(map);
HashMap<String, String> map1 = (HashMap<String, String>) nmt.getObj();
//map1.get;
}
}
警告啊。[/quote]
那就只能加注解了 [/quote]怎么加注解?
import java.util.HashMap;
public class NewMainTest<T> {
private T obj;
public void setObj(T obj) {
this.obj = obj;
}
public T getObj() {
return obj;
}
/**
* @param jsonArray the command line arguments
*/
public static void main(String args[]) throws Exception {
NewMainTest nmt = new NewMainTest<>();
HashMap<String, String> map = new HashMap<>();
nmt.setObj(map);
HashMap<String, String> map1 = (HashMap<String, String>) nmt.getObj();
//map1.get;
}
}
警告啊。[/quote]
那就只能加注解了
import java.util.HashMap;
public class NewMainTest<T> {
private T obj;
public void setObj(T obj) {
this.obj = obj;
}
public T getObj() {
return obj;
}
/**
* @param jsonArray the command line arguments
*/
public static void main(String args[]) throws Exception {
NewMainTest nmt = new NewMainTest<>();
HashMap<String, String> map = new HashMap<>();
nmt.setObj(map);
HashMap<String, String> map1 = (HashMap<String, String>) nmt.getObj();
//map1.get;
}
}
警告啊。