【问题标题】:Having the data's location in the memory but not the data?有数据在内存中的位置但没有数据?
【发布时间】:2014-12-16 19:26:34
【问题描述】:

下面的这个方法用于打印链表中的 Person 对象。但是当我在命令提示符下运行它时,它没有给出 person 参数,而是给出了位置。比如“Person@14991ad”。我做错了什么?

public class PhoneBook
{

    Scanner input = new Scanner(System.in);
    SLinkedList<Person> phoneList;
    Node<Person> newNode;

    public PhoneBook(){
        phoneList = new SLinkedList<Person>();
    }
public void printList(){
    if(phoneList.size() == 0){
        System.out.println("No Record is Found.");
    }

    Node tempNode = phoneList.head;
    for(int i = 1; i <= phoneList.size; i++) {
        System.out.print(tempNode.getElement());

        if(i != phoneList.size)
        System.out.println(" ");
        tempNode = tempNode.getNext();
    }

    System.out.println();
}
}

下面是一个节点类。

  public class Node<E> {

  private E element;
  private Node<E> next;

  public Node() {
    this(null, null);
  }

  public Node(E e, Node<E> n) {
    element = e;
    next = n;
  }

  public E getElement() {
    return element; 
  }
  public Node<E> getNext() { 
    return next;
  }

  public void setElement(E newElem) { 
    element = newElem; 
  }
  public void setNext(Node<E> newNext) {
    next = newNext; 
  }
}

我的链表实现。

public class SLinkedList<E> implements LinkedList<E> {
  public Node<E> head;      
  public Node<E> tail;      
  public int size;      

    public SLinkedList() {
    head = null;
    tail = null;
    size = 0;
  }

  public int size() { 
    return size;
  }

  public boolean isEmpty() {
    return size == 0;
  }

  public void addFirst(Node<E> newNode) {
    if(size == 0) 
        tail = newNode;

    newNode.setNext(head);
    head = newNode;
    size++;
  }

  public void addLast(Node<E> newNode) {
    newNode.setNext(null);

    if(size == 0) 
        head = newNode;

    if (size != 0) 
        tail.setNext(newNode);

    tail = newNode;
    size++;
  }

  public Node<E> removeFirst() {
    Node<E> tempNode = null;
    if (size != 0) {
        if(size == 1)
            tail = null;

        tempNode = head;
        head = head.getNext();
        tempNode.setNext(null);
        size--;
    }

    return tempNode; 

  }

  public Node<E> removeLast() {
    Node tempNode = head;

    if(size == 0)
        return null;

    if(size == 1) {
        head = null;
        tail = null;
        size--;
        return tempNode;
    }


    for(int i=1; i<=size-2; i++) {
        tempNode = tempNode.getNext();
    }

    Node tempNode2 = tail;
    tail = tempNode;
    tail.setNext(null);
    size--;
    return tempNode2;

  }

  public int searchList(E searchKey) {
    if(size == 0)
        return -1;

    Node tempNode = head;
    for(int i=1; i<=size; i++) {
        if(tempNode.getElement().equals(searchKey))
            return i; 
        tempNode = tempNode.getNext();
    }

    return -1;
  }

  public void printList() {
    Node tempNode = head;
    for(int i=1; i<=size; i++) {
        System.out.print(tempNode.getElement());
        if(i!=size) //if it is not last element
            System.out.print(" - ");
        tempNode = tempNode.getNext();
    }
    System.out.println();

  }

}

Person 类。

public class Person
{
    private String name;
    private String surname;
    public  String address;
    public int cell;
    public int home;
    public int work;


    public Person(){

    }

    public Person(String pName, String pSurname, String a, int cNumber, int hNumber, int wNumber)
    {
        name    = pName;
        surname = pSurname;
        address = a;
        cell    = cNumber;
        home    = hNumber;
        work    = wNumber;
    }

    // Accessor methods:
    public String getName(){
        return name;
    }
    public String getSurname(){
        return surname;
    }
    public String getAddress(){
        return address;
    }
    public int getCell(){
        return cell;
    }
    public int getHome(){
        return home;
    }
    public int getWork(){
        return work;
    }

    // Modifier methods:
    public  void setName(String name){
        this.name = name;
    }
    public void setSurname(String surname){
        this.surname = surname;
    }
    public void setAddress (String address){
        this.address = address;
    }
    public void setCell (int cell){
        this.cell = cell;
    }
    public void setHome (int home){
        this.home = home;
    }
    public void setWork (int work){
        this.work = work;
    }



}

【问题讨论】:

  • 下次请使用SSCCE。我们不需要查看所有内容 - 将其精简到导致相同错误的原因并提交。
  • 我以后会小心的谢谢。

标签: java memory methods linked-list location


【解决方案1】:

您没有显示Person 类的代码,但您似乎没有覆盖Object's toString() method,它负责您看到的输出。这不是内存地址,但它确实包含对象的哈希码。

换句话说,这个方法返回一个字符串等于:

getClass().getName() + '@' + Integer.toHexString(hashCode())

Person 类中,覆盖toString() 并返回您希望在打印Person 对象时看到的String

【讨论】:

  • 谢谢!!它有效,但现在还有另一个问题。它没有给出我输入的数据。它给出“ null null null 0 0 0 ”。
  • 我看不到您的 Person 对象在哪里初始化,但您可能使用了无参数构造函数并且没有调用 setter 方法。但如果不是这种情况,那么您可以在这个问题之外提出另一个问题。
猜你喜欢
  • 2019-04-26
  • 2020-02-04
  • 2016-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多