gpt4 book ai didi

java - 内存中有数据的位置但没有数据?

转载 作者:行者123 更新时间:2023-12-01 12:07:08 25 4
gpt4 key购买 nike

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



}

最佳答案

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

In other words, this method returns a string equal to the value of:

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

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

关于java - 内存中有数据的位置但没有数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27512387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com