如何使用Java泛型来避免强制转换?

2024-04-30

对于查询,提出于link https://stackoverflow.com/questions/26192111/how-to-compare-objects-with-different-types,建议使用 Java 泛型以避免难以评估项目的运行时类型。

在下面的代码中使用 Java 泛型后,我没有看到与以前不同的不兼容类型错误。

但是不同的编译时错误line 96 , DList1<int> l = new DList1<int>();没有给出问题的任何线索。

错误信息是:Syntax error on token 'int'

/* DList1.java */

/**
 *  A DList1 is a mutable doubly-linked list.  (No sentinel, not
 *  circularly linked.)
 */

public class DList1<T> {

  /**
   *  head references the first node.
   *  tail references the last node.
   *
   *  DO NOT CHANGE THE FOLLOWING FIELD DECLARATIONS.
   */

  protected DListNode1<T> head;
  protected DListNode1<T> tail;
  protected long size;

  /* DList1 invariants:
   *  1)  head.prev == null.
   *  2)  tail.next == null.
   *  3)  For any DListNode1 x in a DList, if x.next == y and x.next != null,
   *      then y.prev == x.
   *  4)  For any DListNode1 x in a DList, if x.prev == y and x.prev != null,
   *      then y.next == x.
   *  5)  The tail can be accessed from the head by a sequence of "next"
   *      references.
   *  6)  size is the number of DListNode1s that can be accessed from the
   *      head by a sequence of "next" references.
   */

  /**
   *  DList1() constructor for an empty DList1.
   */
  public DList1() {
    this.head = null;
    this.tail = null;
    this.size = 0;
  }


  /**
   *  insertFront() inserts an item at the front of a DList1.
   */
  public void insertFront(T item) {
      if(this.head == null){
          this.head = new DListNode1<T>(item);
          this.tail = this.head;
      }else{
          DListNode1<T> newNode = new DListNode1<T>(item);
          newNode.next = this.head;
          this.head.prev = newNode;
          this.head = newNode;
      }
      this.size++;
  }

  /**
   *  removeFront() removes the first item (and node) from a DList1.  If the
   *  list is empty, do nothing.
   */
  public void removeFront() {
      if(this.size == 0){
          return;
      }else if(size ==1){
          this.head = null;
          this.tail = null;
      }else{
          this.head.next.prev = null;
          this.head = this.head.next;
      }
  }

  /**
   *  toString() returns a String representation of this DList.
   *
   *  DO NOT CHANGE THIS METHOD.
   *
   *  @return a String representation of this DList.
   */
  public String toString() {
    String result = "[  ";
    DListNode1<T> current = head;
    while (current != null) {
      result = result + current.item + "  ";
      current = current.next;
    }
    return result + "]";
  }

  public static void main(String[] args) {
    // DO NOT CHANGE THE FOLLOWING CODE.

    DList1<int> l = new DList1<int>(); //Line 96
    System.out.println("### TESTING insertFront ###\nEmpty list is " + l);

    l.insertFront(9);
    System.out.println("\nInserting 9 at front.\nList with 9 is " + l);
    if (l.head == null) {
      System.out.println("head is null.");
    } else {
        if (l.head.item != 9) { //Line 104
            System.out.println("head.item is wrong.");
        }
        if (l.head.prev != null) {
            System.out.println("head.prev is wrong.");
        }
    }
    if (l.tail == null) {
      System.out.println("tail is null.");
    } else {
      if (l.tail.item != 9) {
        System.out.println("tail.item is wrong.");
      }
      if (l.tail.next != null) {
        System.out.println("tail.next is wrong.");
      }
    }
    if (l.size != 1) {
      System.out.println("size is wrong.");
    }

    l.insertFront(8);
    System.out.println("\nInserting 8 at front.\nList with 8 and 9 is " + l);
    if (l.head == null) {
      System.out.println("head is null.");
    } else {
      if (l.head.item != 8) {
        System.out.println("head.item is wrong.");
      }
      if (l.head.prev != null) {
        System.out.println("head.prev is wrong.");
      }
      if (l.head.next != l.tail) {
        System.out.println("head.next is wrong.");
      }
    }
    if (l.tail == null) {
      System.out.println("tail is null.");
    } else {
      /*if (l.tail.item != 9) {
        System.out.println("tail.item is wrong.");
      }*/
      if (l.tail.next != null) {
        System.out.println("tail.next is wrong.");
      }
      if (l.tail.prev != l.head) {
        System.out.println("tail.prev is wrong.");
      }
    }
    if (l.size != 2) {
      System.out.println("size is wrong.");
    }
  } /* end main() */

}

/* DListNode1.java */

/**
 *  A DListNode1 is a node in a DList1 (doubly-linked list).
 */

class DListNode1<T> {

  /**
   *  item references the item stored in the current node.
   *  prev references the previous node in the DList.
   *  next references the next node in the DList.
   *
   *  DO NOT CHANGE THE FOLLOWING FIELD DECLARATIONS.
   */

  T item;
  DListNode1<T> prev;
  DListNode1<T> next;

  /**
   *  DListNode1() constructor.
   */
  DListNode1() {
    this.item = null;
    this.prev = null;
    this.next = null;
  }

  DListNode1(T item) {
    this.item = item;
    this.prev = null;
    this.next = null;
  }
}

我的问题:

这个错误的含义是什么Line 96?我该如何解决这个问题?

注意:使用jre 1.6更新45


在 Java 中,你不能使用原始值来参数化类型,例如int,所以代替:

DList1<int> l = new DList1<int>();

使用包装类:

DList1<Integer> l = new DList1<Integer>();

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用Java泛型来避免强制转换? 的相关文章

随机推荐