你试了行不行,, 我试了(编译能通过,但不能运行)运行时报错 报错提示: Exception in thread "main" java.lang.ClassCastException: java.lang.Integer at abc.main(abc.java:12)
这样搞,不好吧,,都变成字符串合并了(都不是将单纯取出来的hashmap对象的值变成整型数)
import java.util.*; public class Abc { public static void main(String args[]) { HashMap abc=new HashMap(); abc.put(new Integer(0),new Integer(10)); abc.put(new Integer(2),new Integer(12)); abc.put(new Integer(3),new Integer(13)); System.out.print(abc.get(new Integer(2))); System.out.print((abc.get(1).intValue()+10)); } }
import java.util.*; public class Abc { public static void main(String args[]) { HashMap abc=new HashMap(); abc.put(new Integer(0),new Integer(10)); abc.put(new Integer(2),new Integer(12)); abc.put(new Integer(3),new Integer(13)); System.out.print(abc.get(new Integer(2))); System.out.print((abc.get(new Integer(2)).intValue()+10)); } }
老兄,好像,还是显示这排东西:abc.java:12: cannot find symbol symbol : method intValue() location: class java.lang.Object System.out.print((abc.get(1).intValue()+10)); ^ abc.java:12: operator + cannot be applied to java.lang.Object.intValue,int System.out.print((abc.get(1).intValue()+10)); ^ abc.java:12: reference to print is ambiguous, both method print(char[]) in java.io.PrintStream and method print(java.lang.String) in java.io.PrintStream match System.out.print((abc.get(1).intValue()+10)); ^ Note: abc.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 3 errors
你确定你那边可以正常编译通过,并运行了,显示了结果了吗?我这边总是出现: Abc.java:11: cannot find symbol symbol : method intValue() location: class java.lang.Object System.out.print((abc.get(new Integer(2)).intValue()+10)); ^ Abc.java:11: operator + cannot be applied to java.lang.Object.intValue,int System.out.print((abc.get(new Integer(2)).intValue()+10)); ^ Abc.java:11: reference to print is ambiguous, both method print(char[]) in java.io.PrintStream and method print(java.lang.String) in java.io.PrintStream match System.out.print((abc.get(new Integer(2)).intValue()+10)); ^ Note: Abc.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 3 errors这堆异常~~~~ 烦~~~
应该是:
String.valueOf(10)+abc.get(1);
:)
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer
at abc.main(abc.java:12)
public class Abc
{
public static void main(String args[])
{
HashMap abc=new HashMap();
abc.put(new Integer(0),new Integer(10));
abc.put(new Integer(2),new Integer(12));
abc.put(new Integer(3),new Integer(13));
System.out.print(abc.get(new Integer(2)));
System.out.print((abc.get(1).intValue()+10));
}
}
public class Abc
{
public static void main(String args[])
{
HashMap abc=new HashMap();
abc.put(new Integer(0),new Integer(10));
abc.put(new Integer(2),new Integer(12));
abc.put(new Integer(3),new Integer(13));
System.out.print(abc.get(new Integer(2)));
System.out.print((abc.get(new Integer(2)).intValue()+10));
}
}
symbol : method intValue()
location: class java.lang.Object
System.out.print((abc.get(1).intValue()+10));
^
abc.java:12: operator + cannot be applied to java.lang.Object.intValue,int
System.out.print((abc.get(1).intValue()+10));
^
abc.java:12: reference to print is ambiguous, both method print(char[]) in java.io.PrintStream and method print(java.lang.String) in java.io.PrintStream match
System.out.print((abc.get(1).intValue()+10));
^
Note: abc.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
3 errors
才符合题意,好长时间不写了还真不行
你看第二个这个啊
import java.util.*;
public class Abc
{
public static void main(String args[])
{
HashMap abc=new HashMap();
abc.put(new Integer(0),new Integer(10));
abc.put(new Integer(2),new Integer(12));
abc.put(new Integer(3),new Integer(13));
System.out.print(abc.get(new Integer(2)));
System.out.print((abc.get(new Integer(2)).intValue()+10));
}
}
Abc.java:11: cannot find symbol
symbol : method intValue()
location: class java.lang.Object
System.out.print((abc.get(new Integer(2)).intValue()+10));
^
Abc.java:11: operator + cannot be applied to java.lang.Object.intValue,int
System.out.print((abc.get(new Integer(2)).intValue()+10));
^
Abc.java:11: reference to print is ambiguous, both method print(char[]) in java.io.PrintStream and method print(java.lang.String) in java.io.PrintStream match
System.out.print((abc.get(new Integer(2)).intValue()+10));
^
Note: Abc.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
3 errors这堆异常~~~~ 烦~~~
修改好了
import java.util.*;
public class Abc
{
public static void main(String args[])
{
HashMap abc=new HashMap();
abc.put(new Integer(0),new Integer(10));
abc.put(new Integer(2),new Integer(12));
abc.put(new Integer(3),new Integer(13));
System.out.println(abc.get(new Integer(2)));
System.out.print(((Integer)(abc.get(new Integer(2))).intValue()+10));
}
}
abc.put(1,12);
abc.put(2,13);
老兄,hashmap就没有这样的方法!!!
key和value都必须是Object!!!
import java.util.*;
public class MyClass
{
public static void main(String args[])
{
HashMap abc=new HashMap();
abc.put(new Integer(0),new Integer(10));
abc.put(new Integer(2),new Integer(12));
abc.put(new Integer(3),new Integer(13));
System.out.println(abc.get(new Integer(2)));
System.out.print((((Integer)(abc.get(new Integer(2)))).intValue()+10));
}
}
不好意思
hm.put( new Integer(1),new Integer(1) );
System.out.print( ((Integer)hm.get(new Integer(1))).intValue()+10);