列表元素的通用类
我正在使用通用类练习编程。我想为列表元素实现一个类,该类保存对泛型类型对象的引用和对下一个列表元素的引用。
所以我想出了这个类:
class List<T>{
T val;
List next:
}
如何定义这个类的构造函数?对于理解泛型类及其使用还有其他建议吗?
I am practicing programming with generic classes. I want to implement a class for a list element that holds a reference on an object of generic type and a reference on the next list element.
So I came up with this class:
class List<T>{
T val;
List next:
}
How would you define the constructor for this class? Any other advice to understand generic classes and their use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以将其保留为默认构造函数,然后使用它 <代码>列表<字符串> list = new List(); - 现在类中的 val 将是 String。另一种方式:
然后你可以这样使用它:
结果应该打印“test”
至于建议,我认为最好的办法是阅读这本书:Java 泛型和集合
它包含很好的描述和很多如何使用它的示例。
you can leave it with default constructor and then use it
List<String> list = new List<String>();
- now val in your class will be String. Another way:And then you can use it this way:
as result "test" should be printed
As for advice, I think the best thing is to read this book: Java Generics and Collections
it contains good description and a lot of examples how to use it.
这是一个很好的资源,可以帮助您了解仿制药:
http://www.angelikalanger.com /GenericsFAQ/JavaGenericsFAQ.html
This is a great resource for helping you get your head round generics:
http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html
查看 LinkedList 来了解您想要执行的操作可能会对您有所帮助。
Javadoc: http://download.oracle.com/javase /6/docs/api/java/util/LinkedList.html
源代码和 Java API 文档都可以在此处下载: http:// www.oracle.com/technetwork/java/javase/downloads/index.html
您将能够浏览源代码并了解它是如何使用 Java 中的泛型实际实现的。
It maybe helpful for you to look at a LinkedList for what you're trying to do.
Javadoc: http://download.oracle.com/javase/6/docs/api/java/util/LinkedList.html
Both the source and the Java API docs can be downloaded here: http://www.oracle.com/technetwork/java/javase/downloads/index.html
You'll be able to look through the source and see how it is actually implemented with Generics in Java.
乍一看,我以为您想要更多 LISP 风格的集合,其中有一个“头”元素和一个“尾”列表。将一些代码放在一起给了我这样的结果:
虽然这种结构可以导致对递归算法的一些真正伟大的研究,但当您尝试学习泛型时,它实际上并没有为您做很多事情。也就是说,这是我编写的一个快速测试床,以确保这确实有效:
这至少应该让您更好地了解如何实例化和使用泛型类。
您可以在 http://download.oracle.com/ 找到在线课程javase/tutorial/java/generics/index.html。
关于泛型最需要理解的一点是,当您在类名中列出“T”(如“GenericList”中)时,T 就成为该类范围内的一个类。 GenericList 之外没有任何类知道 T 是什么,并且 GenericList 甚至不知道它是什么 - 它所知道的是,无论你在哪里看到 T,它都会与其他地方的 T 具有相同的类型。因此,虽然 GenericList 不一定知道它存储的内容,但它确实知道“head”(T) 的类型与通过initialList 传入的对象的类型相同。
At first glance, I thought you were going for more of a LISP style collection where you'd have a "head" element and a "tail" list. Tossing together some code gave me this:
While this sort of structure can lead to some really great investigations of recursive algorithms, it really doesn't do a whole heck of a lot for you when you're trying to learn generics. That said, here's a quick test bed I wrote to make sure this actually worked:
That should at least give you a better idea of how to instantiate and utilize a generic class.
You can find an online lesson at http://download.oracle.com/javase/tutorial/java/generics/index.html.
The biggest thing to understand about generics is that, when you list "T" in the name of the class (as in "GenericList"), T becomes a class for the scope of this class. No classes outside of GenericList know what T is and GenericList doesn't even really know what it is - all it knows is that, everywhere you see a T, it'll be the same type as a T somewhere else. So, while GenericList doesn't necessarily know what it's storing, it does know that the type of "head" (T) is the same type as the objects being passed in via initialList.