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?
You could stream the data
array and let Collectors.joining
do the heavy lifting:
@Override
public String toString() {
return Arrays.stream(data)
.mapToObj(Double::toString).collect(Collectors.joining(", ", "[", "]"));
}
Edit:
If you can't stream the array, you could iterate it yourself and give special treatment to the first element, which is the only element that isn't preceded by a comma:
@Override
public String toString() {
StringBuilder sb = new StringBuilder("[");
if (data.length > 0) {
sb.append(data[0]);
}
for (int i = 1; i < data.length; ++i) {
sb.append(", ").append(data[i]);
}
sb.append("]");
return sb.toString();
}
You can stop your loop before the last element of the array and then manually add the last element to your string in the result. Like this:
if (data.length == 0) {
return "[]";
}
int lastElementIndex = data.length - 1;
String result = "[";
for (int i = 0; i < lastElementIndex; i++) {
result += data[i] + ", ";
// or result += String.format("%.1f, ", data[i]); if you are allowed
}
return result + data[lastElementIndex] + "]";
// or String.format("%s%.1f]", result, data[lastElementIndex]);
Just check the length of an array if it is greater than equal to one then concatenate it with toString and then start traversing from index 1 and return it.
public String toString() {
String toString="[";
//check length of an array
if(data.length >= 1){
toString += data[0];
}
//start from 1st index
for(int i=1; i<data.length; i++) {
toString += ", " + data[i];
}
return toString + "]";
}
Why not just check if you are on last element and not add the ,
then:
public String toString2(double[] data) {
String toString = "[";
for (int i = 0; i < data.length; i++) {
if (i == data.length - 1) {
toString = toString + data[i];
} else {
toString = toString + data[i] + ", ";
}
}
return toString + "]";
}
Just add an if-block to check for last element:
public String toString() {
String toString = "[";
for (int i = 0; i < data.length; i++) {
toString = toString + data[i];
if (i != data.length - 1) {
toString = toString + ", ";
}
}
return toString + "]";
}
Or alternatively add your separator before the elements except the first
public String toString() {
String separator = "";
String toString = "[";
for (int i = 0; i < data.length; i++) {
toString = toString + separator;
toString = toString + data[i];
separator = ", ";
}
return toString + "]";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.