Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Demonstration code for blog post 'I tried to print my array but all I got was gobbledegook!'
package com.oopuniversity.reddit.javahelp.array_print;
/**
* Created by OOPUniversity on 11/10/2015.
*/
public class ArrayPrinter {
public static void main(String[] args) {
String[] strs = {"a", "b", "giraffe"};
Integer [] ints = { 1, 2, 37, 4 };
System.out.println(arrayToString(strs));
System.out.println(arrayToString(ints));
}
public static String arrayToString(Object[] theArray) {
StringBuilder output = new StringBuilder("[");
for (Object o:theArray){
if (output.toString().length() > 1) {
output.append(",");
}
output.append(o);
}
output.append("]");
return output.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.