Skip to content

Instantly share code, notes, and snippets.

@OOPUniversity
Created November 10, 2015 16:28
Show Gist options
  • Save OOPUniversity/bada1c9cbd015de97b5b to your computer and use it in GitHub Desktop.
Save OOPUniversity/bada1c9cbd015de97b5b to your computer and use it in GitHub Desktop.
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