交作业~~~

来源:6-5 应用 Collections.sort() 实现 List 排序

慕UI5429127

2019-08-10 13:13

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.Random;


/*

 * 1.创建完List<string>之后,往其中添加十条随机字符串

 * 2.每条字符串的长度为10以内的随机整数

 * 3.每条字符串的每个字符都为随机生成的字符,字符可以重复

 * 4.每条随机字符串不可重复

 */

public class ExceciseCollections {

List<String> stringList=new ArrayList<String>();

public void testSort() {

String a="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

Random random=new Random();

for(int i=0;i<10;i++) {

StringBuilder sb=new StringBuilder();

do {

sb.delete(0, sb.length());

int strlen=random.nextInt(10)+1;

if(strlen==0)continue;

for(int k=0;k<=strlen;k++) {

int cursor=random.nextInt(a.length());

sb.append(a.charAt(cursor));

}

}while(stringList.contains(sb.toString()));

stringList.add(sb.toString());

}

System.out.println("---------排序前------------");

for(String str:stringList) {

System.out.println("序列"+str);

}

Collections.sort(stringList);

System.out.println("-----排序后----------");

for(String str:stringList) {

System.out.println("序列"+str);

}

}


public static void main(String[] args) {

// TODO Auto-generated method stub

ExceciseCollections ec=new ExceciseCollections();

ec.testSort();

}


}


写回答 关注

1回答

  • 慕梦前来
    2022-04-14 18:17:25

    你的这个包的名都没有说清楚呀,这个作业不好

Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409779 学习 · 4341 问题

查看课程

相似问题