如何 return 字符串不多余
How to return string without excess
我有一个用于链表的 toString 方法,它像时尚一样以数组形式打印出来,但它也会打印出多余的字符。我如何摆脱最后一个 ", "
?
public String toString() {
String ret = "[";
Node current = head;
while(current.getNext() != null) {
current = current.getNext();
ret += current.get() + ", ";
}
return ret + "]";
}
示例:我们有一个包含 3 个元素的链表,然后我们将其打印出来。
"[1, 2, 3, ]"
我没有测试过,但应该可以。
public String toString() {
String ret = "[";
Node current = head;
while(current.getNext() != null) {
current = current.getNext();
ret += current.get();
if(current.getNext() != null) ret += ", ";
}
return ret + "]";
}
您可以使用 substring
删除最后 2 个字符。
return ret.substring(0, ret.length()-2)+"]"
public String toString() {
StringBuilder ret = new StringBuilder('[');
Node current = head;
while(current.getNext() != null) {
current = current.getNext();
ret.append(current.get()).append(", ");
}
if (ret.length() > 1) {
ret.delete(ret.length() - 2, ret.length());
}
return ret.append(']').toString();
}
必须警惕一个空列表,所以它仍然是 []
。这留下了两种可能性:布尔标志或之后删除。为了获得更好的性能,请使用 StringBuilder。在某种程度上,编译器会将字符串连接转换为 StringBuilder,但这里并非完全如此。
使用这个ret.substring(0, ret.length() - 1)
@Override
public String toString() {
String ret = "[";
Node current = head;
while(current.getNext() != null) {
current = current.getNext();
ret += current.get() + ", ";
}
ret = ret.substring(0, ret.length() - 1);
return ret + "]";
}
也许您应该考虑看一下 toString()
Java 方法 AbstractCollection
。这将使您了解如何为此目的使用 StringBuilder:
/**
* Returns a string representation of this collection. The string
* representation consists of a list of the collection's elements in the
* order they are returned by its iterator, enclosed in square brackets
* (<tt>"[]"</tt>). Adjacent elements are separated by the characters
* <tt>", "</tt> (comma and space). Elements are converted to strings as
* by {@link String#valueOf(Object)}.
*
* @return a string representation of this collection
*/
public String toString() {
Iterator<E> it = iterator();
if (! it.hasNext())
return "[]";
StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
E e = it.next();
sb.append(e == this ? "(this Collection)" : e);
if (! it.hasNext())
return sb.append(']').toString();
sb.append(',').append(' ');
}
}
试试这个:
public String toString() {
String ret = "[";
Node current = head;
while(current.getNext() != null) {
current = current.getNext();
ret += current.get() + ", ";
}
ret=ret.substring(0,ret.length()-2);
return ret + "]";
}
我使用Apache Commons Lang - StringUtils和ArrayList到return字符串没有多余的。看我怎么用:
public String toString() {
List<String> ret = new ArrayList<String>();
Node current = head;
while (current.getNext() != null) {
current = current.getNext();
ret.add(current.get());
}
return "[" + StringUtils.join(ret, ",") + "]";
}
我有一个用于链表的 toString 方法,它像时尚一样以数组形式打印出来,但它也会打印出多余的字符。我如何摆脱最后一个 ", "
?
public String toString() {
String ret = "[";
Node current = head;
while(current.getNext() != null) {
current = current.getNext();
ret += current.get() + ", ";
}
return ret + "]";
}
示例:我们有一个包含 3 个元素的链表,然后我们将其打印出来。
"[1, 2, 3, ]"
我没有测试过,但应该可以。
public String toString() {
String ret = "[";
Node current = head;
while(current.getNext() != null) {
current = current.getNext();
ret += current.get();
if(current.getNext() != null) ret += ", ";
}
return ret + "]";
}
您可以使用 substring
删除最后 2 个字符。
return ret.substring(0, ret.length()-2)+"]"
public String toString() {
StringBuilder ret = new StringBuilder('[');
Node current = head;
while(current.getNext() != null) {
current = current.getNext();
ret.append(current.get()).append(", ");
}
if (ret.length() > 1) {
ret.delete(ret.length() - 2, ret.length());
}
return ret.append(']').toString();
}
必须警惕一个空列表,所以它仍然是 []
。这留下了两种可能性:布尔标志或之后删除。为了获得更好的性能,请使用 StringBuilder。在某种程度上,编译器会将字符串连接转换为 StringBuilder,但这里并非完全如此。
使用这个ret.substring(0, ret.length() - 1)
@Override
public String toString() {
String ret = "[";
Node current = head;
while(current.getNext() != null) {
current = current.getNext();
ret += current.get() + ", ";
}
ret = ret.substring(0, ret.length() - 1);
return ret + "]";
}
也许您应该考虑看一下 toString()
Java 方法 AbstractCollection
。这将使您了解如何为此目的使用 StringBuilder:
/**
* Returns a string representation of this collection. The string
* representation consists of a list of the collection's elements in the
* order they are returned by its iterator, enclosed in square brackets
* (<tt>"[]"</tt>). Adjacent elements are separated by the characters
* <tt>", "</tt> (comma and space). Elements are converted to strings as
* by {@link String#valueOf(Object)}.
*
* @return a string representation of this collection
*/
public String toString() {
Iterator<E> it = iterator();
if (! it.hasNext())
return "[]";
StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
E e = it.next();
sb.append(e == this ? "(this Collection)" : e);
if (! it.hasNext())
return sb.append(']').toString();
sb.append(',').append(' ');
}
}
试试这个:
public String toString() {
String ret = "[";
Node current = head;
while(current.getNext() != null) {
current = current.getNext();
ret += current.get() + ", ";
}
ret=ret.substring(0,ret.length()-2);
return ret + "]";
}
我使用Apache Commons Lang - StringUtils和ArrayList到return字符串没有多余的。看我怎么用:
public String toString() {
List<String> ret = new ArrayList<String>();
Node current = head;
while (current.getNext() != null) {
current = current.getNext();
ret.add(current.get());
}
return "[" + StringUtils.join(ret, ",") + "]";
}