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

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

請(qǐng)問(wèn)我這個(gè)程序?yàn)槭裁唇Y(jié)果中總是有一個(gè)空的字符串

package?com.imooc.collection;

import?java.util.ArrayList;
import?java.util.Collections;
import?java.util.List;
import?java.util.Random;

/**
?*?將要完成:
?*?1、通過(guò)Collections.sort()方法,對(duì)Integer泛型的List進(jìn)行排序
?*?2、對(duì)String泛型的?List進(jìn)行排序
?*?3、對(duì)其他類型的泛型的例子進(jìn)行排序,一Student類型為例
?*?@author?Administrator
?*
?*/

public?class?CollectionsTest2?{

	public?Random?random?=?new?Random();
	
	
	/**
	?*?創(chuàng)建完List之后往其中添加10條隨機(jī)字符串
	?*?每條字符串的長(zhǎng)度為10以內(nèi)的隨機(jī)整數(shù)
	?*?每條字符串的每個(gè)字符都是隨機(jī)生成的,可以重復(fù)
	?*?每條隨機(jī)字符串不可重復(fù)
	?*?@param?args
	?*/
	
	public?void?testSort3(){
		List<String>?stringList?=?new?ArrayList<String>();
		int?i?=?0;
		do{
			String?str?=?getRandomString();
			if((str==null)||(stringList.contains(str)))
				continue;
			i++;
			stringList.add(str);
			System.out.println("成功添加字符串?:"+str);
		}while(i<9);
		
		System.out.println("-----------排序前--------------");
		for?(String?string?:?stringList)?{
			System.out.println("字符串:"+string);
		}
		
		Collections.sort(stringList);
		System.out.println("-----------排序后--------------");
		for?(String?string?:?stringList)?{
			System.out.println("字符串:"+string);
		}
		
		
	}
	
	/**
	?*?生成一個(gè)長(zhǎng)度隨機(jī)隨機(jī)字符串
	?*?@param?args
	?*/
	public?String?getRandomString(){
		int?length?=?random.nextInt(10);
		//sb用來(lái)存儲(chǔ)每一個(gè)字符串
		StringBuffer?sb?=?new?StringBuffer();
		for?(int?i?=?0;?i?<?length;?i++)?{
			//num=0時(shí)隨機(jī)生成A-Z;當(dāng)num=1時(shí)隨機(jī)生成a-z;當(dāng)num=2時(shí)隨機(jī)生成0-9
			int?num?=?random.nextInt(3);
			long?result?=0;
			switch?(num)?{
			case?0:
				result?=?Math.round(Math.random()*25+65);
				sb.append(String.valueOf(result));
				break;
			case?1:
				result?=?Math.round(Math.random()*25+97);
				sb.append(String.valueOf(result));
				
				break;
			case?2:
				sb.append(random.nextInt(10));?	
				break;

			default:
				break;
			}
		}
		return?sb.toString();
		
	}
	
public?static?void?main(String[]?args)?{
		CollectionsTest?ct?=?new?CollectionsTest();
		ct.testSort3();
	}
}


正在回答

1 回答

int length = random.nextInt(10);

隨機(jī)數(shù)的范圍是[0,10),前閉后開。

當(dāng)length取值為0時(shí),new?StringBuffer().toString()的值就是空值""。

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

田同學(xué) 提問(wèn)者

非常感謝!
2017-05-04 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

請(qǐng)問(wèn)我這個(gè)程序?yàn)槭裁唇Y(jié)果中總是有一個(gè)空的字符串

我要回答 關(guān)注問(wèn)題
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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