亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

作業作業,感覺自己寫的一般

package?com.imooc6;
import?java.util.ArrayList;
import?java.util.Collections;
import?java.util.List;
import?java.util.Random;
????????public?class?CollectionsTesta?{		
????????/**	
????????*?1.創建完List<String>之后,往其中添加十條隨機字符串	
????????*?2,每條字符串的長度為10以內的隨機數	
????????*?3.每條字符串的每個字符都為隨機生產的字符,字符可以重復	
????????*?4.每條隨機字符串不可重復	
????????*/	
??????????????public?void?testString(){		
????????????????List<String>?listString=new?ArrayList<String>();		
????????????????Random?random=new?Random();		
????????????????Random?random1=new?Random();		
????????????????String?strs="0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";		
????????????????StringBuilder?str;		
????????????????int?b;		
????????????????int?a;		
?????????????????????for(int?j=0;j<10;j++){			
?????????????????????????str=new?StringBuilder();			
?????????????????????????b=random.nextInt(10)+1;		
????????????????????????????for(int?i=0;i<b;i++){			
????????????????????????????a=random1.nextInt(62);		
????????????????????????????str.append(strs.charAt(a));		
??????????????????????????}		
??????????????????????????listString.add(str.toString());	
????????????????????????}		
????????????????????????System.out.println("-------排序前-------");		
????????????????????????for?(String?string?:?listString)?{			
???????????????????????????System.out.println("元素:"+string);		
???????????????????????????}		
???????????????????????????Collections.sort(listString);		
???????????????????????????System.out.println("-------排序后-------");		
????????????????????????for?(String?string?:?listString)?{			
????????????????????????System.out.println("元素:"+string);		
????????????????????????}	
??????????????????????}	
??????????????????????/**	
??????????????????????*?@param?args	
??????????????????????*/	
?????????????public?static?void?main(String[]?args)?{	//?TODO?Auto-generated?method?stub		
?????????????
?????????????CollectionsTesta?ct=new?CollectionsTesta();		
?????????????ct.testString();	
?????????????}
?????????????}


正在回答

1 回答

package collection;


import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.Random;

import java.util.UUID;


public class collectionTest {

????public void testSort3() {

????????List<String> stList = new ArrayList<String>();

????????Random random = new Random();

????????String randomStr;

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

????????????int R = random.nextInt(10);

????????????do {

????????????????randomStr = UUID.randomUUID().toString().substring(0, R);

?????????????}while(stList.contains(randomStr));

????????????stList.add(randomStr);

????????????System.out.println("將要添加字符串:"+randomStr);

????????}

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

????????for(String s:stList) {

????????????System.out.println("元素:"+s);

????????}

????????Collections.sort(stList);

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

????????for(String s:stList) {

????????????System.out.println("元素:"+s);

????????}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

collectionTest ct = new collectionTest();

// ct.testSort1();

// ct.testSort2();?

ct.testSort3();

}



0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
Java入門第三季
  • 參與學習       409767    人
  • 解答問題       4543    個

Java中你必須懂得常用技能,不容錯過的精彩,快來加入吧

進入課程

作業作業,感覺自己寫的一般

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號