Java Hashmap 错误:类型不兼容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19230420/
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
Hashmap error: incompatible types
提问by TheEyesHaveIt
I made a hashmap that stores roman numerals as keys and their decimal numbers as values. The error says "incompatible types - found java.lang.Object but expected int". I'm just trying to get the value of the roman numeral when I write "conversions.get(numOne.charAt(x));" What am I doing wrong here?
我制作了一个哈希图,将罗马数字存储为键,将十进制数字存储为值。错误说“不兼容的类型 - 找到 java.lang.Object 但预期为 int”。当我写“conversions.get(numOne.charAt(x));”时,我只是想获得罗马数字的值 我在这里做错了什么?
import java.util.Scanner;
import java.util.HashMap;
public class test
{
static Scanner sc = new Scanner(System.in);
static HashMap conversions = new HashMap();
public static void main(String args[]){
conversions.put('I',1);
conversions.put('V',5);
conversions.put('X',10);
conversions.put('L',50);
conversions.put('C',100);
conversions.put('D',500);
conversions.put('M',1000);
String numOne = "XIX";
for(int x = 0; x <= numOne.length()-2; x++){
int temp1 = conversions.get(numOne.charAt(x));
int temp2 = conversions.get(numOne.charAt(x+1));
}
}
}
采纳答案by cmd
Change the line:
更改行:
static HashMap conversions = new HashMap();
to
到
static Map<Character,Integer> conversions = new HashMap<Character,Integer>();
or as of Java 7, we can avoid some duplication by doing the following
或者从 Java 7 开始,我们可以通过执行以下操作来避免一些重复
static Map<Character,Integer> conversions = new HashMap<>();
All in all, this will autobox your primitives and resolve your problem
总而言之,这将自动装箱您的原语并解决您的问题
回答by Juned Ahsan
HashMap stores and returns Objects and you cannot assign an object to a primtive directly:
HashMap 存储并返回对象,您不能直接将对象分配给原始对象:
int temp1 = conversions.get(numOne.charAt(x));
int temp2 = conversions.get(numOne.charAt(x+1));
try this instead:
试试这个:
int temp1 = (Integer) conversions.get(numOne.charAt(x));
int temp2 = (Integer) conversions.get(numOne.charAt(x+1));
or better use parameterized Map to avoid the cast during retrieval of objects:
或者更好地使用参数化 Map 来避免在检索对象期间进行转换:
static HashMap<Character, Integer> conversions = new HashMap<Character, Integer>();
回答by Joachim Sauer
Making proper use of generics, you can tell Java what your HashMap
is supposed to contain:
正确使用泛型,你可以告诉 Java 你HashMap
应该包含什么:
static HashMap<Character,Integer> conversions = new HashMap<>();
This would make your code compile, as the compiler would know that conversions.get()
returns an Integer
(and that can automatically be converted to an int
via auto-unboxing).
这将使您的代码编译,因为编译器会知道conversions.get()
返回一个Integer
(并且可以int
通过自动拆箱自动转换为一个)。
Prior to Java 7, you'd need to repeat the types for the initialization as well, making it new HashMap<Character,Integer>()
.
在 Java 7 之前,您还需要重复初始化的类型,使其new HashMap<Character,Integer>()
.
回答by stinepike
you need to cast the value to an object. As others already suggested use like
您需要将值转换为对象。正如其他人已经建议使用一样
for(int x = 0; x <= numOne.length()-2; x++){
int temp1 = (Integer) conversions.get(numOne.charAt(x));
int temp2 = (Integer) conversions.get(numOne.charAt(x+1));
}
With this I also recommend to declare the Hashmap like following
有了这个,我还建议像下面这样声明 Hashmap
static Map<String, Integer> conversions = new HashMap<String, Integer>();
回答by nini
for(int x = 0; x <= numOne.length()-2; x++){
int temp1 = (Integer) conversions.get(numOne.charAt(x));
int temp2 = (Integer) conversions.get(numOne.charAt(x+1));
System.out.println(temp1 + " "+temp2);
Use this, output is -
使用这个,输出是 -
10 1 1 10
10 1 1 10