要是在toString()中使用循环,建议用下面的方法
public class UsingStringBuilder {
public static Random rand = new Random(47);
public String toString(){
StringBuilder result = new StringBuilder("[");
for (int i = 0; i <10 ; i++) {
result.append(rand.nextInt(100));
result.append(",");
}
result.append("]");
return result.toString();
}
public static void main(String[] args) {
UsingStringBuilder usb = new UsingStringBuilder();
System.out.println(usb);
}
}
输出:
[58,55,93,61,61,29,68,0,22,7,]