用Java打印对象数组

时间:2014-04-09 16:45:35

标签: java

我正在制作电话簿并填写条目。这些条目包括两个姓氏和姓名字符串以及一个电话号码。我正在使用数组来存储条目。我正在尝试将数组打印出来,并且我在每个类中都放了toString方法。但是当我打印出来时,我仍然得到“[LEntry; @ 8dc8569”。我不确定我做错了什么。这是代码。

public class Entry {

String surname;
String initial;
int number;


public Entry(String surname, String initial, int number) {
    this.surname = surname;
    this.initial = initial;
    this.number = number;
}

public String getSurname(){
    return surname;
}

public String getInitial(){
    return initial;
}

public int getNumber() {
    return number;
}

void setNumber(int number){
    this.number = number;
}

public String toString(){
    return surname+ "\t" +initial+ "\t" +number;
}
}


public class ArrayDirectory {

int DIRECTORY_SIZE = 6; 
Entry [] directory = new Entry[DIRECTORY_SIZE];


public void addEntry(String surname, String initial, int num) {

    int i = findFreeLocation();               

    directory[i] = new Entry(surname, initial, num);

}


public void deleteEntry(String surname, String initial) {

    int i = findEntryIndex(surname, initial);                     
    directory[i] = null;
}


public void deleteEntry(int number) {

    int i = findEntryIndex(number);                     
    directory[i] = null;
}


public int findEntry(String surname, String initial) {

    int i;
    i = findEntryIndex(surname, initial);

    return directory[i].getNumber();
}


public void editNum(String surname, String initial, int number) {

    int i;
    i = findEntryIndex(surname, initial);
    directory[i].setNumber(number);
}


public void print() {
    // TODO print array
    System.out.println(directory);
}

private int findEntryIndex(String surname, String initial) {
    int i;
    for (i = 0; i <= DIRECTORY_SIZE; i++) 
    {
        if(directory[i] != null && directory[i].getSurname().equals(surname) && directory[i].getInitial().equals(initial))
        { 
            break;
        }
    }
    return i;
}

private int findEntryIndex(int number) {
    int i;
    for (i = 0; i <= DIRECTORY_SIZE; i++)
    {
        if(directory[i] != null && directory[i].getNumber() == number)
        { 
            break;
        }
    }
    return i;
}


private int findFreeLocation() {
    int i;
    for (i = 0; i < DIRECTORY_SIZE; i++) 
    {                                           
        if(directory[i] == null)
        { 
            break;
        }
    }
    return i;
}

public String toString() {

    for(int i = 0 ; i< DIRECTORY_SIZE ; i++){ 
        System.out.println( directory[i] );
    }
    return null;

}


}


public class Test {

public static void main(String[] args) {

    ArrayDirectory phoneBook = new ArrayDirectory();

    phoneBook.addEntry("Bigger", "R", 2486);
    phoneBook.addEntry("Smaller", "E", 0423);
    phoneBook.addEntry("Ringer", "J", 6589);
    phoneBook.addEntry("Looper", "T", 6723);
    phoneBook.addEntry("Lennon", "B", 4893);
    phoneBook.addEntry("Martin", "M", 2121);

    phoneBook.print();

}

}

1 个答案:

答案 0 :(得分:4)

使用Arrays.toString();

Arrays.toString(directory); 

当您只打印directory(类型Entry的数组实例)时,它不会像您期望的那样覆盖toString()方法


另见