使用 int 作为 java.util.Dictionary 的类型参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2001755/
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
Using int as a type parameter for java.util.Dictionary
提问by Richard Szalay
When I try to declare a Dictionary as such:
当我尝试这样声明字典时:
private Dictionary<String, int> map;
The compiler gives me the following error:
编译器给了我以下错误:
Syntax error on token "int", Dimensions expected after this token
标记“int”上的语法错误,此标记后的预期尺寸
But it works fine with Integer
. I'm vaguely aware that Java treats int
/ Integer
differently (I come from a .NET background), but I was hoping someone could give me a full explanation on why I can't use primitives in a Dictionary<>
但它与Integer
. 我隐约知道 Java 对待int
/ 的方式Integer
不同(我来自 .NET 背景),但我希望有人能给我一个完整的解释,说明为什么我不能在 Dictionary<> 中使用原语
采纳答案by Jeremy Raymond
In Java primitives aren't objects, so you can't use them in place of objects. However Java will automatically box/unbox primitives (aka autoboxing) into objects so you can do things like:
在 Java 中,原语不是对象,所以不能用它们代替对象。然而,Java 会自动将原语(又名autoboxing)装箱/拆箱到对象中,因此您可以执行以下操作:
List<Integer> intList = new LinkedList<Integer>();
intList.add(1);
intList.add(new Integer(2));
...
Integer first = intList.get(0);
int second = intList.get(1);
But this is really just the compiler automatically converting types for you.
但这实际上只是编译器自动为您转换类型。
回答by TofuBeer
Java collections only allow references not primitives. You need to use the wrapper classes (in this case java.lang.Integer) to do what you are after:
Java 集合只允许引用而不是原语。您需要使用包装类(在本例中为 java.lang.Integer)来完成您的任务:
private Dictionary<String, Integer> map;
they you can do things like:
他们可以执行以下操作:
int foo = map.get("hello");
and
和
map.put("world", 42);
and Java uses autoboxing/unboxing to deal with the details of the conversion for you.
Java 使用自动装箱/拆箱来为您处理转换的细节。
Hereis a little description on it.
这里是关于它的一些描述。
回答by Y. Shoham
Because in Java the primitives are truely primitives. In Java int
will pass by value, while Integer
will pass a reference. In .NET int or Int32 etc. are just different names.
因为在 Java 中,原语是真正的原语。在 Java 中int
将按值传递,而Integer
将传递引用。在 .NET 中 int 或 Int32 等只是不同的名称。
回答by JohnE
In .Net, "primitive" types are backed by objects. In Java, there's a hard distinction between primitive types and Objects. Java 5 introduced autoboxing, which can coerce between the two in certain situations. However, because the Java generics system uses type-erasure, there isn't enough information to autobox in this case.
在 .Net 中,“原始”类型由对象支持。在 Java 中,原始类型和对象之间存在严格的区别。Java 5 引入了自动装箱,它可以在某些情况下在两者之间进行强制。但是,由于 Java 泛型系统使用类型擦除,因此在这种情况下没有足够的信息进行自动装箱。
回答by Keibosh
To expand on TofuBeer's answer.
扩展 TofuBeer 的答案。
int is a primitive
int 是一个原语
Integer is an Object.
整数是一个对象。
Generics does not support primitives.
泛型不支持原语。
回答by Abs
@XmlJavaTypeAdapter(value=MyAdapter.class, type=int.class)
Thats the trick specify type to make it work with primitives
这就是指定类型以使其与原语一起工作的技巧
In your adapter
在您的适配器中
using the same in package-info will mean you do it globally for that package
在 package-info 中使用相同的将意味着您对该包进行全局操作
Found this after experimenting.
实验后发现这个。
public class MyAdapter extends XmlAdapter<String, Integer> {