« Least Recently Used (LRU) Cache Implementation (Java) | Main | Software Campfire Stories: HashMap Ate My Objects (Java) »

04/30/2012

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00e5501e33db88340168eaf63c8b970c

Listed below are links to weblogs that reference JDK Source Browsing: Interesting Way To Format An Array To String:

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

Sai Matam

Nitin - All your comments are there. I did not delete any of them. What is the bug that you are talking about?

Thank you everyone for the comments and the lively discussion. After all your suggestions, the code now is in great shape :-)

regards,
Sai Matam

Nitin

My earlier comment got deleted ! it is easy (well not much easy when you get excited about improvements) to find the bug :)

Nitin

Code in JDK and by nicerobot is 'correct', all other versions presented here have bug !
Be careful while doing so called optimization ;)

Luca Garulli

Just a little improvement: don't compute size -1 every time but only once. For the rest is perfect.

public static String toStringMyOwnVersion(int[] a) {
if (a == null) {
return "null";
}

if (a.length == 0) {
return "[]";
}

StringBuilder sb = new StringBuilder();
db.append('[');

int size = a.length-1;
for (int x=0; x < size; x++) {
sb.append(a[x]).append(", ");
}

sb.append(a[size]).append(']');

return sb.toString();
}

nicerobot

// You should always pre-allocate StringBuilders.
// A loops condition and increment should always be as simple as possible.

public static String arrayToString(int[] a) {
if (a == null) {
return "null";
}

int size = a.length;
if (size == 0) {
return "[]";
}

StringBuilder sb
= new StringBuilder(size*4+2)
.append('[').append(a[0]);

for (int x=1; x < size; x++) {
sb.append(", ").append(a[x]);
}

return sb.append(']').toString();
}

Sumit

Use Google Guava.

Eric Jablow

This is too much work. Since your separator is "," instead of ", ", you cannot use java.lang.Arrays.toString(int[]). But you can save yourself a lot of code with this:

public static String toCommaSeparatedString(int[] a) {
if (a == null) {
return null;
}
String separator = "";
StringBuilder b = new StringBuilder();
b.append("[");
for(int i: a) {
b.append(separator);
b.append(i);
separator = ",";
}
b.append("]");
return b.toString();
}

There's only one if statement. In fact, I've seen a library, I think from Israel, with a Separator class:

public class Separator {
private final String separator;
private String current = "";
public Separator(final String separator) {
this.separator = separator;
}
public String toString() {
String s = current;
current = separator;
return s;
}
}

Now, the code becomes:


public static String toCommaSeparatedString(int[] a) {
if (a == null) {
return null;
}
Separator separator = new Separator(",");
StringBuilder b = new StringBuilder();
b.append("[");
for(int i: a) {
b.append(separator);
b.append(i);
}
b.append("]");
return b.toString();
}

I'd be careful about using Separator though; using an IDE and debugging might trigger the toString() method early.

David Karr

Your version could be improved somewhat by using StringBuilder instead of StringBuffer. The latter is thread-safe, which isn't necessary when your buffer is used entirely within a single method.

evernat

Personally, I would prefer your version, because it is clearer in particular for the return part.

But my own version would be the following, keeping some append(char) from the JDK version as general tip on performance:

public static String toStringMyOwnVersion(int[] a) {
if (a == null) {
return "null";
}

if (a.length == 0) {
return "[]";
}

StringBuilder sb = new StringBuilder();
sb.append('[');

int size = a.length;
for (int x=0; x < size-1; x++) {
sb.append(a[x]).append(", ");
}

sb.append(a[size-1]).append(']');

return sb.toString();
}

What is sure is that they all work well, and they probably have negligible performance differences.

The comments to this entry are closed.