Skip to content

Instantly share code, notes, and snippets.

@t0dd
Created October 30, 2012 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t0dd/3982607 to your computer and use it in GitHub Desktop.
Save t0dd/3982607 to your computer and use it in GitHub Desktop.
Java custom ArrayList toString()
//A custom toString method that returns a meaningful string representation of an ArrayList object
public String toString() {
String strOutput = "MyArrayListObject ["; //Prints constructor name + left bracket
for(int i = 0; i < myArr.size(); i++){ //myArr = ArrayList instance variable
strOutput += myArr.get(i) + ", ";
}
strOutput = strOutput + "]";
return strOutput;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment