Java-声明新的泛型集时出现意外类型错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19530816/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:04:23  来源:igfitidea点击:

Java- Getting unexpected type error when declaring new generic set

javagenerics

提问by Van

I thought I knew what I was doing with generics, but apparently not.

我以为我知道我在用泛型做什么,但显然不知道。

ArraySetList<char> setA = new ArraySetList<char>();

When compiled gives:

编译时给出:

error: unexpected type
ArraySetList<char> setA = new ArraySetList<char>();
             ^
required: reference
found:    char

As well as the same error for all subsequent char's. I'm wondering how to declare a new ArraySetList of characters.

以及所有后续字符的相同错误。我想知道如何声明一个新的 ArraySetList 字符。

Here are all my files.

这是我所有的文件。

http://pastebin.com/4h37Xvu4     // ArraySetList (extends ArrayUnsortedList)
http://pastebin.com/FxmynzkC     // Driver
http://pastebin.com/CgVA0zjY     //ArrayUnsortedList (implements ListInterface)
http://pastebin.com/3iXrCsCc     //ListInterface\

采纳答案by Little Child

Java Generics work for objects and not for primitive data types. If you, however, need to store primitive data types, you will need to use their corresponding wrapper class objects.
These classes just "wrap" around the primitive data type to give them an object appearance.

Java泛型适用于对象而不是原始数据类型。但是,如果您需要存储原始数据类型,则需要使用它们对应的包装类对象。
这些类只是围绕原始数据类型“包装”以赋予它们对象外观。

For char, the corresponding wrapper class is Characterand hence, you must write your line of code as so:

对于char,相应的包装类是Character,因此,您必须按如下方式编写代码行:

ArraySetList<Character> setA = new ArraySetList<Character>();   

Please read: http://docs.oracle.com/javase/tutorial/java/data/numberclasses.html

请阅读:http: //docs.oracle.com/javase/tutorial/java/data/numberclasses.html

When you add elements, however, you will add normal char. That is because Java will automatically convert it into Characterfor you and back to charautomatically, if need be. This is called auto-boxing conversion.

但是,当您添加元素时,您将添加普通char. 这是因为Java会自动将其转换成Character你和回char自动,如果需要的话。这称为自动装箱转换。

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.

自动装箱是 Java 编译器在原始类型与其对应的对象包装类之间进行的自动转换。例如,将 int 转换为 Integer,将 double 转换为 Double,等等。如果转换以另一种方式进行,则称为拆箱。

source: http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

来源:http: //docs.oracle.com/javase/tutorial/java/data/autoboxing.html

回答by Reimeus

Generic type arguments require reference types (or wilcards).

泛型类型参数需要引用类型(或通配符)。

You can't use primitive types (for more see restrictions);

您不能使用原始类型(有关更多信息,请参阅限制);

ArraySetList<Character> setA = new ArraySetList<Character>();

Read JLS 4.5.1 Type Arguments and Wildcardsfor usable types

阅读JLS 4.5.1可用类型的类型参数和通配符