【问题标题】:Accessing an object's method from a List.从 List 访问对象的方法。
【发布时间】:2014-03-28 18:14:54
【问题描述】:

下面是我的 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;
    }
}

【问题讨论】:

  • 项目是Object。它没有任何getItem() 方法。
  • insertend 中的其他人似乎有点粗略?它将temp 添加到tail.next,然后将temp.prev 设置为tail,然后使tail 也成为temp

标签: java list


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    您将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 {
        ...
    }
    

    然后你必须解决所有问题,比如在你真正想要tobject 的地方设置int

    【讨论】:

    • 并非所有添加到DList 的项目都是tobject 类型,所以这是无效的
    猜你喜欢
    • 2014-07-25
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    相关资源
    最近更新 更多