your toString function is incorrect. If the
arr has only one item, then the function will throw
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
here is how it should go;
if there is nothing in the array then print
{}
if one element only then print
{element1}
if more than one then print
{element1, element2}
so, suggested algorithm will be
string := "{"
if element count is zero then
string := concat(string, "}");
return
end if
string := concat(string, arr[0]);
while ( item in array, skipping the first element ): do
string := concat(string, ", ", item);
end while
string := concat(string, "}")
try your source code