从List访问对象的方法。

时间:2014-03-28 18:14:55

标签: java list

下面是我的DList代码,它接收对象。它打印出一切都很好,除了最后一个打印声明。 "的System.out.println((int)的(list.head.next.next.next.item.getItem()));"这是为什么?以及如何解决它?

/* DList1.java */

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

public class DList1 {

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

  protected DListNode1 head;
  protected DListNode1 tail;
  protected long size;



  public DList1() {
    head = null;
    tail = null;
    size = 0;

  }



  public DList1(Object a) {
    head = new DListNode1();
    tail = head;
    head.item = a;
    size = 1;
  }  



  public DList1(Object a, Object b) {
    head = new DListNode1();
    head.item = a;
    tail = new DListNode1();
    tail.item = b;
    head.next = tail;
    tail.prev = head;
    size = 2;
  }

  public void insertFront(Object i) {
    DListNode1 temp = new DListNode1(i);
    if (size == 0) {
      head = temp;
      tail = temp;
    }
    else {
      temp.next = head;
      head.prev = temp;
      head = temp;
    } size++;  
  }

  public void insertEnd(Object i) {
    DListNode1 temp = new DListNode1(i);
    if (size == 0) {
      head = temp;
      tail = temp;
    }
    else {
      tail.next = temp;
    temp.prev = tail;
      tail = temp;    
    } size++;  
  }

  public void removeFront() {
    if (size == 0) {
      return;
    }
    else if (size == 1) {
      head = null;
      tail = null;
      size--;
    }
    else {
      head = head.next;
      head.prev = null;
      size--;
    }
  }


  public String toString() {
    String result = "[  ";
    DListNode1 current = head;
    while (current != null) {
      result = result + current.item + "  ";
      current = current.next;
    }
    return result + "]";
  }

  public static void main(String[] args) {
      int[] array = new int[2];
      array[0] = 3;
      array[1] = 4;
      int m = 1;
      String g = "hi";
      String s = "boo";
      String z = "foo";
      DList1 list = new DList1(m);
      tobject jim = new tobject();

      //list.insertFront(g);
      list.insertEnd(s);
      list.insertEnd(g);
      list.insertEnd(jim);
      System.out.println((list.head.item));
      System.out.println((list.head.next.item));
      System.out.println((list.head.next.next.item));
      System.out.println((int)(list.head.next.next.next.item.getItem()));
      System.out.println(list.size);
      //System.out.println((int)(list.head.next.item[0]));// expected 3 but failed
      //System.out.println(((String) list.head.next.item));

  }

}
//////////////
/* DListNode1.java */

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

public class DListNode1{

  /**
   *  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.
   */

  public Object item;
  public DListNode1 prev;
  public DListNode1 next;

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

  DListNode1(Object i) {
    item = i;
    prev = null;
    next = null;
      }
    }
////////////
public class tobject {
    private int pai;

    public tobject(){
        pai = 3;
    }
    public int getItem(){
        return pai;
    }
}

2 个答案:

答案 0 :(得分:0)

问题是,您首先需要将返回的值投射到tobject类型,然后才能调用getItem()中定义的特殊tobject方法。然后,您可以将结果转换为int,但println()足够聪明,可以在"期望"中打印任何Object或原语。没有铸造的方式。

System.out.println(((tobject)list.head.next.next.next.item).getItem());

但是,通常试图预先了解项目在列表中的类型是很难管理的。这就是为什么generics可以派上用场的原因,但它确实意味着您应该尝试将列表中的事物类型保持为相同的类型或子类型。

答案 1 :(得分:0)

您是否有理由在节点中将item定义为对象? 项目是Object。它没有任何getItem()方法。您的数据结构不应该更像这样吗?:

public class DListNode1{
    public tobject item;
    public DListNode1 prev;
    public DListNode1 next;
    ...
    DListNode1(tobject i) {
        item = i;
        prev = null;
        next = null;
    }
}

//You should use uppercase to name class
public class tobject {
    ...
}

然后你必须解决所有问题,例如设置int你真正想拥有的tobject

相关问题