Java 泛型参数

发布于 2024-10-20 11:20:08 字数 2274 浏览 6 评论 0原文

在这里回顾一下我的基本 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜雨飘雪 2024-10-27 11:20:08

如果您希望 NodeList 和 MyList 仅包含 Comparable 项,
您可以将通用参数 T 替换为以下内容:

public class NodeList<T extends Comparable> {

或者

public class NodeList<T extends Comparable<T>> {

并将使用 Comparable 的位置替换为 T。这样,您就知道 T 至少实现了 Comparable 的方法。

Oracle 的泛型教程应该能够帮助您获得他们的悬挂。


您可能遇到的一个问题是您从静态函数引用成员变量,例如在 leftHalf 中:

   for ( int countToMiddle = 0; countToMiddle < middle ; countToMiddle++ ) {
      leftSide.addEnd(nodes);
    }

nodes 是一个成员变量,即非静态变量,所以你不能从静态方法中调用它。对于该示例,您必须从传递的 MyList 中获取它:

   for ( int countToMiddle = 0; countToMiddle < middle ; countToMiddle++ ) {
      leftSide.addEnd(list.nodes);
    }

对于尝试使用成员变量的其他静态方法也是如此。


另外,您收到类似错误的原因是: addEnd(S) in MyList不能应用于 (NodeList)是因为根据您的类型参数,S 是一个 Comparable。 NodeList 不扩展 Comparable!

您拥有的两个解决方案是

  1. 使 NodeList 扩展 Comparable,以便您可以将其传递给 MyList.addEnd
  2. 为接受 NodeList 的 addEnd 进行重载(即具有相同名称的不同方法),并添加所有将 NodeList 中的项目传递给 MyList

或者提出更适合您的类的需求的不同解决方案。


虽然我意识到您正在实现一个链接列表只是为了提高您的面试技能(祝您好运!),但我只想补充一点,有一个通用的 LinkedList 已在 Java 中可用。

If you want NodeList and MyList to only contain Comparable items,
you can replace the generic parameter T with something like:

public class NodeList<T extends Comparable> {

Or

public class NodeList<T extends Comparable<T>> {

And replace where you use Comparable with T. 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:

   for ( int countToMiddle = 0; countToMiddle < middle ; countToMiddle++ ) {
      leftSide.addEnd(nodes);
    }

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 passed MyList:

   for ( int countToMiddle = 0; countToMiddle < middle ; countToMiddle++ ) {
      leftSide.addEnd(list.nodes);
    }

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 because S is, according to your type parameter, a Comparable. NodeList does not extend Comparable!

The two solutions you have is

  1. Make NodeList extend Comparable so you can pass it to MyList.addEnd
  2. Make an overload (i.e. a different method with the same name) for addEnd that takes a NodeList, and add all the items in the passed NodeList to MyList

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.

贱人配狗天长地久 2024-10-27 11:20:08

为什么几乎相同的问题要发两次?
您可以扩展您的问题,添加评论等。

我们已经给您提示。 :)

Why do you post almost the same question twice?
You could extend your question, add comments etc.

We already gave you that hint. :)

捂风挽笑 2024-10-27 11:20:08

发生错误的原因是 NodeList 类没有接收通用 T 类和 NodeList 的构造函数。
实际上,此实现将替换节点在每个循环中引用的引用对象。你也应该解决这个问题。

您应该做的是将 T 本身设置为 Comparable,并更改属性,例如:

public class NodeList<T extends Comparable> {
    private T head;
    private NodeList tail;
    public NodeList( T item, NodeList list ) {
        head = item;
        tail = list;
    }
}

如果您告诉我们代码的确切用途,那就更好了。

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:

public class NodeList<T extends Comparable> {
    private T head;
    private NodeList tail;
    public NodeList( T item, NodeList list ) {
        head = item;
        tail = list;
    }
}

It would be better if you tell us what exactly the code is for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文