如何打印数组的内容

时间:2014-04-26 14:19:12

标签: java arrays

我目前正在测试我的ArrayPhoneDirectory文件,该文件涉及我测试该文件中列出的所有单个方法。 在测试了我的addChangeEntry方法之后,我需要打印出我的数组(newdir)的内容,以便检查我添加的3个联系人是否存在,但我不确定最好的方法。

ArrayPhoneDirectoryTester代码:

public class ArrayPhoneDirectoryTester {
public static void main (String[] args)
{
    //creates a new PhoneDirectory
    PhoneDirectory newdir = new ArrayPhoneDirectory();

    System.out.println("ArrayPhoneDirectoryTester Running");
    System.out.println("********************************* \n");

    System.out.println("TEST 1: addChangeEntry");
    System.out.println("********************** \n");

    newdir.addChangeEntry("Joe Perkins", "07762573872");
    newdir.addChangeEntry("Annie Howell", "0876273862");
    newdir.addChangeEntry("Buzz Killington", "99999999999");


    // PRINT ARRAY TO PROVE THEY HAVE BEEN ADDED

对此的任何帮助都将非常感激。

编辑添加代码:

ArrayPhoneDirectory代码:

import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ArrayPhoneDirectory implements PhoneDirectory {

private static final int INIT_CAPACITY = 100;
private int capacity = INIT_CAPACITY;

//holds telno of directory entries
private int size = 0;

//Array to contain directory entries
private DirectoryEntry[] theDirectory = new DirectoryEntry[capacity];


//Holds name of data file to be read
private String sourceName = null;

/**
 * Flag to indicate whether directory was modified since it was last loaded
 * or saved.
 */
private boolean modified = false;

// PUBLIC INTERFACE METHODS

public void loadData(String sourceName) {
    Scanner scan = new Scanner(sourceName).useDelimiter("\\Z");

    while (scan.hasNextLine()) {
        String name = scan.nextLine();
        String telno = scan.nextLine();

        add(name, telno);
    }
}

/**
 * find method is called, returning the position in the array of the given
 * name.
 */
public String lookUpEntry(String name) {
    find(name);
    return null;

}

/**
 * for loop that checks every DirectoryEntry inside theDirectory and then
 * checks it against the name and telno given in the parameter, if both are
 * equal, the telno is updated, else it is added to theDirectory
 *
 */

public String addChangeEntry(String name, String telno) {
    try {
        for (DirectoryEntry x : theDirectory) {
            if (x.getName().equals(name)) {
                x.setNumber(telno);
                return x.getNumber();
            }
            add(name, telno);  
        }

    }
    catch (NullPointerException e) {
        System.err.println("NullPointerExcepttion : " + e.getMessage());

    }

    finally {


    }
    return null;
}



//TO COMPLETE
public String removeEntry(String name) {
    return null;
}

/**
 * A new PrintWriter object is created, and a for loop is used to print the
 * name and number of each DirectoryEntry to the console.
 */
public void save() {
    PrintWriter pw = null;
    try {
        pw = new PrintWriter(new FileWriter("directory.txt", true)); 

        for (DirectoryEntry x : theDirectory) {
            pw.write(x.getName());
            pw.write(x.getNumber());               
        }
    }
    catch(Exception e) {
        e.printStackTrace();
    }
    finally {
        pw.close();    
    }
}




//Private helper methods

private void reallocate() {
    capacity = capacity * 2;
    DirectoryEntry[] newDirectory = new DirectoryEntry[capacity];
    System.arraycopy(theDirectory, 0, newDirectory,
            0, theDirectory.length);

    theDirectory = newDirectory;
}

private void add(String name, String telno) {
    if (size >= capacity) {
        reallocate();
    }
    theDirectory[size] = new DirectoryEntry(name, telno);
    size = size + 1;
}

private int find(String name) {
    int i = 0;
    for (DirectoryEntry x : theDirectory) {
        if (x.getName().equals(name)) {
            return i;
        }
        i++;
    }
    return -1;
}

@Override
public String format() {
    return null;
}

}

PhoneDirectory代码:

public interface PhoneDirectory {

/**
 * Load file containing directory entries
 *
 * @param sourceName is name of the file containing the directory entries
 */
void loadData(String sourceName);

/**
 * Look up an entry.
 *
 * @param name The name of person to look up
 * @return The telno or null if name is not in the directory
 */
String lookUpEntry(String name);

/**
 * Add new entry or change an existing entry.
 *
 * @param name The name of the person being added or whose telno is going to
 * change
 * @param telno The telno being changed or added
 * @return The old telno or if a new entry null
 */
String addChangeEntry(String name, String telno);

/**
 * Remove an entry from the directory.
 *
 * @param name The name of the person to be removed
 * @return The current telno. If not in the directory, null is returned.
 */
String removeEntry(String name);

/**
 * If the directory has been modified the contents of the directory are
 * written back to the file
 */
void save();

/**
 * Builds a single string representing directory contents Each entry is
 * terminated by a new line
 *
 * @return The formatted list of directory contents
 */
String format();

}

1 个答案:

答案 0 :(得分:0)

我建议你使用JUnit测试和断言来检查上瘾,而不是打印到控制台。无论如何,最简单的方法可能是实现toString的{​​{1}}方法,然后调用ArrayPhoneDirectory。在System.out.println(newdir.toString())内,您只需toStringArrays.toString(_theArray_)

编辑:根据您已展示的代码,最好执行以下操作:

Arrays.deepToString(_theArray)

如果您愿意,当然可以Map<String, String> testValues = new HashMap<String, String(); testValues.put("Joe Perkins", "07762573872"); testValues.put("Annie Howell", "0876273862"); testValues.put("Buzz Killington", "99999999999"); for(String key : testValues.keySet()) { newdir.addChangeEntry(key, testValues.get(key)); } for(String key : testValues.keySet()) { assert testValues.get(key).equals(newdir.lookUpEntry(key)); } 代替System.out.println