Java 泛型参数
在这里回顾一下我的基本 ADT 内容,为面试进行修改,并尝试通过学习 Java 来一石二鸟。尝试使用通用链表(我自己创建)编写一个简单的合并排序算法。事实证明,这比我最初想象的要困难得多!有人可以帮我吗?我将开始研究基础知识,并在进一步了解后更新这篇文章。
我的通用链表代码如下:
public class NodeList<T extends Comparable<T> > {
private T head;
public NodeList<T> tail;
public NodeList( T item, NodeList<T> list ) {
head = item;
tail = list;
}
}
我正在尝试在我制作的另一个类中访问此类,如下所示:
public class MyList<T extends Comparable<T>> {
private NodeList<T> nodes;
private static int size;
public MyList( ) {
nodes = null;
}
public MyList(T[] array ){
for( T item : array ) {
nodes = new NodeList<T>(item, nodes);
}
size = array.length;
}
public void add( T item ) {
nodes = new NodeList<T>( item, nodes );
size++;
}
public void addEnd( T item ) {
NodeList<T> temp = nodes;
while ( temp == null || temp.tail != null) {
temp = temp.tail;
}
size++;
temp.tail = new NodeList<T> ( item, null);
}
我相信到目前为止,一切都是正确的,直到 add 和 addEnd 方法,这应该分别将泛型添加到列表的开头和列表的末尾。
我的代码继续:
public static <S extends Comparable<S>>
MyList<S> sort( MyList<S> list ) {
if ( size > 1 ) {
MyList<S> left = leftHalf( list );
MyList<S> right = rightHalf( list );
list = merge( left, right );
}
return list;
}
private static <S extends Comparable<S>>
MyList<S> merge( MyList<S> left, MyList<S> right ) {
}
private static <S extends Comparable<S>>
MyList<S> leftHalf( MyList<S> list ) {
MyList <S> leftSide = new MyList();
int middle;
if(size % 2 == 1) {
middle = size +1;
} else {
middle = size;
}
for ( int countToMiddle = 0; countToMiddle < middle ; countToMiddle++ ) {
leftSide.addEnd(nodes);
}
// return elements from 0 .. list.size() / 2
}
我收到错误:
MyList 中的 addEnd(S) 无法应用于 (NodeList)
我运行时发生的
(NodeList)leftSide.addEnd(节点);
任何人都可以看到其中的原因/告诉我到目前为止我的工作是否正确?再次非常感谢!
Going back over my basic ADT stuff here to revise for an interview, and trying to kill two birds with one stone by learning Java while I am. Attempting to write a simple algorithm for a merge sort with a generic linked list ( which I am creating myself). It's proving to be far more difficult than I had first imagined ! Can anyone help me out please ? I will start out working on the basics and will update this post as I get further in.
My code for the generic linked list is as follows :
public class NodeList<T extends Comparable<T> > {
private T head;
public NodeList<T> tail;
public NodeList( T item, NodeList<T> list ) {
head = item;
tail = list;
}
}
I am trying to access this class in another class I have made, which is as follows :
public class MyList<T extends Comparable<T>> {
private NodeList<T> nodes;
private static int size;
public MyList( ) {
nodes = null;
}
public MyList(T[] array ){
for( T item : array ) {
nodes = new NodeList<T>(item, nodes);
}
size = array.length;
}
public void add( T item ) {
nodes = new NodeList<T>( item, nodes );
size++;
}
public void addEnd( T item ) {
NodeList<T> temp = nodes;
while ( temp == null || temp.tail != null) {
temp = temp.tail;
}
size++;
temp.tail = new NodeList<T> ( item, null);
}
I believe, so far, everything to be correct up until the add and addEnd methods, which should add a generic to the start of the list and end of the list respectively.
My code continues with :
public static <S extends Comparable<S>>
MyList<S> sort( MyList<S> list ) {
if ( size > 1 ) {
MyList<S> left = leftHalf( list );
MyList<S> right = rightHalf( list );
list = merge( left, right );
}
return list;
}
private static <S extends Comparable<S>>
MyList<S> merge( MyList<S> left, MyList<S> right ) {
}
private static <S extends Comparable<S>>
MyList<S> leftHalf( MyList<S> list ) {
MyList <S> leftSide = new MyList();
int middle;
if(size % 2 == 1) {
middle = size +1;
} else {
middle = size;
}
for ( int countToMiddle = 0; countToMiddle < middle ; countToMiddle++ ) {
leftSide.addEnd(nodes);
}
// return elements from 0 .. list.size() / 2
}
And I get the error:
addEnd(S) in MyList cannot be applied to (NodeList)
which occurs when I run
leftSide.addEnd(nodes);
Can anyone see a reason for this/ tell me if I am correct up to this point of my work ? Thanks so much again!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您希望 NodeList 和 MyList 仅包含
Comparable
项,您可以将通用参数
T
替换为以下内容:或者
并将使用
Comparable
的位置替换为T
。这样,您就知道 T 至少实现了 Comparable 的方法。Oracle 的泛型教程应该能够帮助您获得他们的悬挂。
您可能遇到的一个问题是您从静态函数引用成员变量,例如在
leftHalf
中:nodes
是一个成员变量,即非静态变量,所以你不能从静态方法中调用它。对于该示例,您必须从传递的 MyList 中获取它:对于尝试使用成员变量的其他静态方法也是如此。
另外,您收到类似错误的原因是:
addEnd(S) in MyList
不能应用于 (NodeList) 是因为根据您的类型参数,S
是一个 Comparable。NodeList
不扩展 Comparable!您拥有的两个解决方案是
MyList.addEnd
或者提出更适合您的类的需求的不同解决方案。
虽然我意识到您正在实现一个链接列表只是为了提高您的面试技能(祝您好运!),但我只想补充一点,有一个通用的 LinkedList 已在 Java 中可用。
If you want NodeList and MyList to only contain
Comparable
items,you can replace the generic parameter
T
with something like:Or
And replace where you use
Comparable
withT
. This way, you know T at least implements Comparable's methods.Oracle's tutorials for generics should be able to help you with getting the hang of them.
One problem you may be having is that you refer to member variables from static functions, like in
leftHalf
you have:nodes
is a member variable, i.e. a non-static variable, so you can't call it from static methods. For that example, you'd have to get it from the passedMyList
:And the same goes for your other static methods that try to use member variables.
Also, the reason you are getting an error like:
addEnd(S) in MyList<S> cannot be applied to (NodeList<T>)
is becauseS
is, according to your type parameter, a Comparable.NodeList
does not extend Comparable!The two solutions you have is
MyList.addEnd
Or come up with a different solution that better fits the need of your classes.
While I realize you are implementing a linked list just to sharpen your skills for an interview (I wish you good luck!), I just want to add that there is a generified LinkedList already available in Java.
为什么几乎相同的问题要发两次?
您可以扩展您的问题,添加评论等。
我们已经给您提示。 :)
Why do you post almost the same question twice?
You could extend your question, add comments etc.
We already gave you that hint. :)
发生错误的原因是 NodeList 类没有接收通用 T 类和 NodeList 的构造函数。
实际上,此实现将替换节点在每个循环中引用的引用对象。你也应该解决这个问题。
您应该做的是将 T 本身设置为 Comparable,并更改属性,例如:
如果您告诉我们代码的确切用途,那就更好了。
The error is happening because the class NodeList doesn't have a constructor that receives a generic T class and a NodeList.
Actually, this implementation will replace the reference object that nodes is referring on every loop. You should also fix that.
What you should do is put T to be a Comparable itself, and change the attribute, something like:
It would be better if you tell us what exactly the code is for.