第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定

生成隨機(jī)字符串并排序

標(biāo)簽:
Java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class collectionstest2 {
public List<student>studentlist=new ArrayList<student>();//学生对象
public List<StringBuilder>stringlist=new ArrayList<StringBuilder>();

Random random=new Random();
//对类对象的排序(随机生成不同的ID排序)
public void test(){
int k,a,b,c,d,e;

        k=random.nextInt(10);

    studentlist.add(new student(k+"","bobay"));

        a=random.nextInt(10);
    if(a!=k){

    studentlist.add(new student(a+"","ann"));
    }else{this.random.nextInt();}
        b=random.nextInt(10);

    if(b!=k&&b!=a){
    studentlist.add(new student(b+"","ken"));
    }else{
        this.random.nextInt();
    }
        c=random.nextInt(10);
    if(c!=k&&c!=a&&c!=b){

    studentlist.add(new student(c+"","mike"));
    }else{
        this.random.nextInt();
    }
    System.out.println("-----------排序前------------");
    for(student s:studentlist){
        System.out.println("学生:"+s.id+" "+s.name);
    }
    Collections.sort(studentlist);
    System.out.println("-----------排序后------------");
    for(student s:studentlist){
        System.out.println("学生:"+s.id+" "+s.name);
    }

}
//对字符串排序

public void stringSort(){

for(int i=0;i<10;i++){//控制字符串个数
String str="abcdefghilmnopqrstuvwxyzABCDEFGHILMNOPQRSTUVWXYZ0123456789";
int b=str.length();
StringBuilder sb=new StringBuilder();
int d=random.nextInt(10);
for(int a=0;a<d;a++){
sb.append(str.charAt(random.nextInt(b)));
}
sb.toString();
stringlist.add(sb);

}
System.out.println("--------------排序前--------------");
for(StringBuilder string:stringlist){
System.out.println("字符:"+string);
}
Collections.sort(stringlist, new comparatortest())
;//这里需要新建类创建一个comparator方法:
/*
import java.util.Comparator;

public class comparatortest implements Comparator<StringBuilder> {

@Override
public int compare(StringBuilder o1, StringBuilder o2) {
    // TODO Auto-generated method stub
    return o1.toString().compareTo(o2.toString());
}

}

*/
System.out.println("--------------排序后--------------");
for(StringBuilder string:stringlist){
System.out.println("字符:"+string);
}
}
public static void main(String[] args) {
collectionstest2 c1=new collectionstest2();
c1.test();
c1.stringSort();
}
}

點(diǎn)擊查看更多內(nèi)容
1人點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消