手记

Java入门第三季 6-5课后练习

新手小白,望大神指导!

public void testSort4(){
List<String> stringList = new ArrayList<String>();
String stringBase = "0123456789abcdefghijklmnopqrstuvwxyz"

  • "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    String sb ="" ;
    char[] ch;
    Random random = new Random();
    for (int i = 0; i < 10; i++) {
    do{
    int temp = random.nextInt(10);
    ch = new char[temp];
    for(int j = 0;j < temp;j++ ){
    int temp2 = random.nextInt(stringBase.length());
    char ch2 = stringBase.charAt(temp2);
    ch[j] = ch2;
    }
    sb = sb.valueOf(ch);
    }while(stringList.contains(sb));

        System.out.println("将要添加的字符串是:"+sb);
        stringList.add(sb);
    }
    System.out.println("-----------排序前---------------");
    for (String string : stringList) {
        System.out.println("元素:"+string);
    }
    System.out.println("-----------排序后---------------");
    Collections.sort(stringList);
    for (String string : stringList) {
        System.out.println("元素:"+string);
    }

    }

2人推荐
随时随地看视频
慕课网APP

热门评论

学多久啦?这么厉害。

查看全部评论