源自:6-5 应用 Collections.sort() 实现 List 排序
怎么去掉重复和空字符串?
public void testSort3(){
List<String> strList = new ArrayList<String>();
Random random = new Random();
String strings ="zxcvbnmasdfghjklpoiuytrewqZXCVBNMLKJHGFDSAQWERTYUIOP01234567890";
StringBuilder newString = new StringBuilder();//村村字符串
String s = new String();
for(int i =0;i<10;i++){
int stringLength =0;
if(stringLength==0){
//生成字符串长度
stringLength = random.nextInt(10)+1;
}
/**do{
stringLength = random.nextInt(10);
}while(stringLength==0);*/
do{
for(int j=0;j<stringLength;j++){
//1.每条字符串的每个字符都为随机生成的字符,字符可以重复
//int index = (int)(Math.random()*62);
//2.随机产生字符串
int r = random.nextInt(62);
//获取每个单个字符
char char1 =strings.charAt(r);
//往newString追加字符
newString.append(char1);
}
}while(strList.contains(newString));
//转换成字符串
s = newString.toString();
//strList.add(newString.toString());
strList.add(s);
System.out.println("成功添加字符串"+i+":"+s);
//
newString.delete(0, newString.length());
}
System.out.println("------排序前------");
for (String string : strList) {
System.out.println("元素:"+string);
}
Collections.sort(strList);
System.out.println("------排序后------");
for (String string : strList) {
System.out.println("字符串:"+string);
}
}
提问者:寤悟勿真
2019-08-09 20:02