有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

排序如何在Java中对自定义泛型类型链表进行排序?

我正在用java编写属于泛型的链表,而不是使用java集合链表。链表的添加方法由以下代码组成:

public void add(T item, int position) {
  Node<T> addThis = new Node<T>(item);
  Node<T> prev = head;
  int i;

  if(position <= 0) {
    System.out.println("Error: Cannot add element before position 1.");
  }

  else if(position == 1) {
    addThis.setNext(head);
    head = addThis;
  } else {
    for(i = 1; i < position-1; i++) {
      prev = prev.getNext();
      if(prev == null) {
        System.out.println("Cannot add beyond end of list");
      }
    } // end for
    addThis.setNext(prev.getNext());
    prev.setNext(addThis);
  }
} // end add

我怎样才能在添加新项目时,将该项目与另一个项目进行比较,并按字母顺序插入?我已经研究过使用compareTo,但我不知道该怎么做

谢谢

编辑: 我有各种各样的类:我有一个名为Dvd的类,它有一个标题(字符串)的方法和变量,以及该标题的副本数(int)。我还有一个linked list class,一个listinterface,一个node class,和一个main class


共 (2) 个答案

  1. # 1 楼答案

    你提到使用泛型,但又提到按字母顺序排序。泛型不一定是字符串,它们被用来表示任何类型,而像字母顺序这样的排序属性意味着字母字符。我的回答假设您期望类型为T的泛型对象具有字母性质。在我的示例中,我专门使用String

    您可以设置代码来搜索要添加的位置,而不是提供它

    public void add(T item) {
        Node<T> addThis = new Node<T>(item);
        Node<T> itr = head;
    
        while (itr.hasNext()) {
            if (addThis.compareTo(itr.getNext()) <= 0) { // itr > addThis
                addThis.setNext(itr.getNext());
                itr.setNext(addThis);
                return;
            }
            itr = itr.getNext();
        }
        addThis.setNext(null);
        itr.setNext(addThis);
        return;
    } // end add
    

    然后在Node类中,可以实现Interface Comparable。我假设你存储了一个字符串,因为你问过字母排序This Question解释按字母顺序比较字符串

    class Node implements Comparable<Node> {
    
    
        String value;  // ASSUMING YOU ARE USING A STRING AS YOUR GENERIC TYPE T
    
        @Override
        public int compareTo(Node otherNode) {
            int i;
            String thisString = this.getValue();
            String otherString = otherNode.getValue();
            int minSize = ( otherString.length() > thisString.length() ? thisString.length() : otherString.length() );
            for (i = 0; i < minSize; i++) {
                 if (thisString.charAt(i) > otherString.charAt(i)) {
                     return 1;
                 } else if (thisString.charAt(i) < otherString.charAt(i)) {
                     return -1;
                 }
            }
            if (otherString.length() > thisString.length()) {
                return 1;
            } else if (otherString.length() < thisString.length()) {
                return -1;
            } else {
                return 0;
            }
        }
    
        // OTHER CLASS CONSTRUCTORS, VARIABLES, AND METHODS
    }
    

    为了使用简单的泛型实现这一点,您需要使用类型T实现Comparable来实现Node类,如下所示:

    class NodeNode<T extends Comparable<T>> implements Comparable {
    
    
        T value;
    
        @Override
        public int compareTo(Node otherNode) {
            return this.getValue().compareTo(otherNode.getValue());
        }
    
        // OTHER CLASS CONSTRUCTORS, VARIABLES, AND METHODS
    }
    
  2. # 2 楼答案

    您的实现是否扩展了java。util。列表界面

    可以简单地将对象添加到列表中,然后使用集合对列表进行排序吗。排序()