哪位大神指导一下关于创建对象的位置StringBuilder sr=new StringBuilder();
public void testString() {
List<String> xs=new ArrayList<String>();
String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random=new Random();
for(int i=0;i<10;i++) {
StringBuilder sr=new StringBuilder();
do {
int num1=random.nextInt(10)+1;
for(int y=0;y<num1;y++) {
int num= random.nextInt(62);
sr.append(str.charAt(num));
}}while(xs.contains(sr));
xs.add(sr.toString());
}
System.out.println("-------排序前------------");
for (String string : xs) {
System.out.println(string);
}
System.out.println("-------------排序后-------------------------");
Collections.sort(xs);
for (String string : xs) {
System.out.println(string);
}
为什么StringBuilder sr=new StringBuilder();只能在for循环里面创建,如果在for外面创建的话生成的随机字符串他就会自动排序好了?这是为什么?