将数据的位置放在内存中而不是数据中?

时间:2014-12-16 19:26:35

标签: java memory methods linked-list location

下面这个方法用于打印链接列表中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;
    }



}

1 个答案:

答案 0 :(得分:1)

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

  

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

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

Person班级中,覆盖toString()并返回打印String对象时要查看的Person