How do I format a String to convert a double array to the form: [0.0, 2.4, 5.5, 6.3]?
I am assigning double array elements to one string to have a string representation in the format above.
My code to change double array to a single string representation:
public String toString() {
String toString="[";
for(int i=0; i<data.length; i++) {
toString = toString + data[i] + ", ";
}
return toString + "]";
}
I am getting the array in the format [0.0, 2.4, 5.5, 6.3, ], which is not expected and wrong.
How can I get it formatted to [0.0, 2.4, 5.5, 6.3]?
I have tried to use Arrays.toString() method but, I am not allowed to use that in this project.
How should I change my code to have the desired format?