数据的位置在内存中,但没有数据?



下面的此方法用于打印链表中的内容,即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();
  }
}

人员类。

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;
    }

}

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

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

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

Person 类中,重写toString()并返回打印 Person 对象时要查看的String

相关内容

  • 没有找到相关文章

最新更新