qq_是我_13
2019-06-29 21:11
public void testSort2() throws IOException {
List<String> rList=new ArrayList<String>();
Random random=new Random();
String s;
char a[] = null;
Scanner console=new Scanner(System.in);
int L=random.nextInt(10);
for(int i=0;i<10;i++) {
for(int w=0;w<L;w++) {
a[w]=(char)System.in.read();
}
s=new String(a);
rList.add(s);
System.out.print("已经成功添加"+s);
for (String c : rList) {
System.out.print("元素"+c);
}
Collections.sort(rList);
for (String c : rList) {
System.out.print("元素"+c);
}
}
}
public void testStringSort() { List<String> stringList = new ArrayList<String>(); String stringSource = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; char[] stringArr = stringSource.toCharArray(); Random random = new Random(); for(int i=0;i<10;i++) { StringBuilder str = new StringBuilder(""); int h = (int)(Math.random()*10+1); do { for(int p = 0;p<h;p++) { int num = (int)(Math.random() * stringArr.length); str.append(stringArr[num]); } }while(stringList.contains(str)); String strNew = ""+str; stringList.add(strNew); } System.out.println("-----------排序前--------------------"); for(String str1:stringList) { System.out.println("元素:"+str1); } Collections.sort(stringList); System.out.println("-----------排序后--------------------"); for(String str2:stringList) { System.out.println("元素:"+str2); } }
我是这么写的
Java入门第三季
409792 学习 · 4340 问题
相似问题