java - 对象的哈希码数组

标签 java arrays hashcode

<分区>

我想将链表转换为数组。但是当我打印它时,它会给我对象的哈希码。我怎样才能解决这个问题?我创建一个数组,然后将其与链表中的 person 对象相等。

Person [] personArray;
personArray = new Person[phoneList.size()];
System.out.println(personArray);

电话簿类:

public class PhoneBook
{

Scanner input = new Scanner(System.in);
SLinkedList<Person> phoneList;
Person [] personArray;

public PhoneBook(){
    phoneList = new SLinkedList<Person>();
}

public void addPerson(){
    System.out.println("Create new person.");
    System.out.println("Name: ");
    String name = input.next();

    System.out.println("Surname: ");
    String sn = input.next();

    System.out.println("Address: ");
    String a = input.next();

    System.out.println("Cell Number: ");
    int cell = input.nextInt();

    System.out.println("Home Number: ");
    int hn = input.nextInt();

    System.out.println("Work Number: ");
    int wn = input.nextInt();

    Person per = new Person(name, sn, a, cell, hn, wn); 
    phoneList.addLast(per);
    personArray = new Person[phoneList.size()];
    System.out.println(personArray);

}

人物类:

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


    public Person(String name, String surname, String address, int cell, int home, int work){
        this.name    = name;
        this.surname = surname;
        this.address = address;
        this.cell    = cell;
        this.home    = home;
        this.work    = work;
    }

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

    public String toString(){
        return name + " " + surname + " " + address + " " + cell + " " + home + " " + work;
    }
}

最佳答案

使用Arrays.toString:

personArray = new Person[phoneList.size()];
//here you should add code that actually copies the elements of the list to 
//the array. Otherwise, the array would be empty
System.out.println(Arrays.toString(personArray));

关于java - 对象的哈希码数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27524298/

相关文章:

java - 使用 OpenOffice SDK 创建数据源时抛出异常

java - Libgdx 如何实现选中按钮列表?

javascript - 通过其他数组从数组过滤器中删除记录

两个数组上的 Javascript Map 和 Filter

arrays - 为什么这会创建一个二维数组 - Excel VBA

java - 如何为子类编写hashCode()

java - 如何规范化/抛光 Java 中的文本?

java - 调用 registerBroadcast 时出现 NullPointerException

java - 当同一程序在 JVM5 和 JVM6 中运行时,HashMap 中的项目顺序不同

java - 在二维数组中查找相似行的代码