【问题标题】:Object's Hash Code Array [duplicate]对象的哈希码数组 [重复]
【发布时间】:2014-12-17 11:12:22
【问题描述】:

我想将链表转换为数组。但是当我打印它时,它会给我对象的哈希码。我怎样才能解决这个问题?我创建了一个数组,然后将其与链表中的 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;
    }
}

【问题讨论】:

标签: java arrays hashcode


【解决方案1】:

使用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));

【讨论】:

    猜你喜欢
    • 2018-12-04
    • 1970-01-01
    • 2020-10-11
    • 2010-11-26
    • 2011-01-04
    • 1970-01-01
    • 2019-05-21
    • 2013-08-28
    • 1970-01-01
    相关资源
    最近更新 更多