Reputation: 2315
I was having some problem when trying to format the string. So basically I got enqueue and dequeue and it was working fine now. Here is the code:
public String toString(){
String str = "[";
for(int i = front; i<= rear; i++){
str += Q[i] + ",";
}
if(str.length() > 0 && str.charAt(str.length()-1)==','){
str.substring(0, str.length()-1);
}
return str;
}
I wanted the result to be like [1,2,3,4]. However, I have no idea how to replace the last "," to "]". The current problem is it returns me [1,2,3,4, and that's it.
Upvotes: 1
Views: 136
Reputation: 7743
There is the new, Java 8 only style:
Arrays.stream(Arrays.copyOfRange(Q, front, rear)).collect(Collectors.joining(",", "[", "]");
Which implicitly creates a StringJoiner with the parameters passed to Collectors.joining(), delimiter, prefix, suffix. While not specifically answering your question as it is phrased, it solves the actual problem you're facing.
Upvotes: 0
Reputation: 124215
First of all, don't use concatenation +=
inside loops
for (...){
//...
resultString += newString
//...
}
to create string result. This way each time you execute this code it creates new String which needs to copy value from resultString
and add newString
.
Instead use proper tools like StringBuilder
and its append
method. This prevents copying previous result each time (which can take some time) and places newString in bigger buffer. So make your code more like
StringBuilder sb = new StringBuilder();
for (...){
sb.append(newString);
}
String result = sb.toString();
Also if you want to add delimiter like ,
you can use new StringJoiner(delimiter)
(added in Java 8) or if you want to add prefix and suffix like [
and ]
use new StringJoinder(delimiter, prefix, suffix)
.
So your code can look more or less like:
StringJoiner sj = new StringJoiner(",", "[", "]");
for(int i = front; i<= rear; i++){
sj.add(Q[i]);
}
return sj.toString();
or even
return IntStream
.rangeClosed(fron, rear)
.mapToObj(i->Q[i])
.collect(Collectors.joining(",", "[", "]"));
Upvotes: 1
Reputation: 393771
How about :
String str = "[";
for(int i = front; i< rear; i++){
str += Q[i] + ",";
}
str += Q[rear] + "]";
Of course, I'd use StringBuilder instead of String concatenation :
StringBuilder str = new StringBuilder(128);
str.append('[');
for(int i = front; i< rear; i++){
str.append(Q[i]);
str.append(',');
}
if (rear > front) {
str.append(Q[rear]);
}
str.append(']');
return str.toString();
Upvotes: 1
Reputation: 300
public String toString(){
String str = "[";
for(int i = front; i<= rear; i++){
if (i == rear)
str += Q[i] + ",";
else
str += Q[i] + "]";
}
return str;
}
Upvotes: 1
Reputation: 4817
change :
for(int i = front; i<= rear; i++){
str += Q[i] + ",";
}
to :
for(int i = front; i< rear; i++){
str += Q[i] + ",";
}
str+=Q[rear] + "]";
also it is better to use StringBuilder.append()
instead of str +=
, Because String is immutable
and every time you use str+=
it creates new instance of your str
Object
with StringBuilder
it will be :
StringBuilder str = new StringBuilder();
str.append("[");
for(int i = front; i<= rear; i++){
str.append(Q[i] + ",");
}
str.append(Q[rear] + "]");
Upvotes: 3
Reputation: 174696
Use string.replaceAll
string.replaceAll(",(?=[^,]*$)", "]");
OR
string.replaceAll("(?m),$", "]");
Upvotes: 1