Created
November 10, 2015 16:28
-
-
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!'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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