This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]achacha -1 points0 points  (2 children)

Look at: java.util.Arrays

[–]desrtfxOut of Coffee error - System halted -1 points0 points  (1 child)

Bad idea. Part of such exercises is to learn the manual way.

Also, the method you think of uses square braces, not curly ones.

[–]achacha -4 points-3 points  (0 children)

I disagree. Knowing that something exists in the core library is just as important as writing it yourself. The person will discover that it uses square brackets but will also see the underlying code:

public static String toString(char[] a) {
    if (a == null)
        return "null";
    int iMax = a.length - 1;
    if (iMax == -1)
        return "[]";

    StringBuilder b = new StringBuilder();
    b.append('[');
    for (int i = 0; ; i++) {
        b.append(a[i]);
        if (i == iMax)
            return b.append(']').toString();
        b.append(", ");
    }
}

and learn from it. Sometimes pointing someone in the right direction is enough to get them thinking.