Java通用参数 [英] Java generic arguments

查看:213
本文介绍了Java通用参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里回顾我的基本ADT内容以进行采访修改,并在我学习Java的同时尝试用一块石头杀死两只鸟.尝试使用通用链表(我正在创建自己)编写用于合并排序的简单算法.事实证明,这比我最初想象的要困难得多!有人可以帮我吗?我将开始研究基础知识,并随着我的深入而更新这篇文章.

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);
  }

到目前为止,我相信所有要纠正的事情,直到add和addEnd方法为止,这应该分别将泛型添加到列表的开头和列表的结尾.

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.

我的代码继续:

 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)

addEnd(S) in MyList cannot be applied to (NodeList)

我跑步时会发生

leftSide.addEnd(nodes);

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!

推荐答案

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

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> {

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

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

And replace where you use Comparable with T. This way, you know T at least implements Comparable's methods.

Oracle的泛型教程应该可以为您提供帮助抓住他们的窍门.

Oracle's tutorials for generics should be able to help you with getting the hang of them.

您可能遇到的一个问题是,您引用了静态函数中的成员变量,就像在leftHalf中那样:

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是成员变量,即非静态变量,因此您不能从静态方法中调用它.对于该示例,您必须从传递的MyList中获取它:

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.

此外,出现诸如addEnd(S) in MyList<S> cannot be applied to (NodeList<T>)之类的错误的原因是,根据类型参数,S是可比较的. NodeList不扩展Comparable!

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. 使NodeList扩展为Comparable,以便将其传递给MyList.addEnd
  2. 对采用NodeList的addEnd进行重载(即,使用相同名称的不同方法),然后将传递的NodeList中的所有项添加到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.

这篇关于Java通用参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆