本節課程代碼+注釋
在課后作業中,在生成字符的集合時有參考其它慕課同學的寫法,借鑒了一個字符類型集合的字符添加方法,并靈活使用了參數列表,佩服。
貼出我的代碼,有機會讓別的童鞋看看,換位想一下,有機會一起交流。
package?com.imooc.season3.CollectionDemo;
import?java.util.ArrayList;
import?java.util.Collections;
import?java.util.List;
import?java.util.Random;
import?java.util.Scanner;
import?java.util.stream.IntStream;
/*
?*?運用Collections.sort(List?list)對集合中的元素進行排序
?*?public?class?Collections?extends?Object
?*?This?class?consists?exclusively?of?static?methods?that?operate?on?or?return?collections.?
?*?①對Integer類的list集進行排序
?*?----10個隨機整數,打印出排序前和排序后
?*?
?*?②對String類的list集進行排序
?*?----人手輸入3個字符串,并打印出排序前和排序后
?*?
?*?③作業,存儲隨機生成的String類的list集并對其進行排序
?*?----創建List<String>?str集合后,往其添加十條隨機字符串
?*?----每條字符串的長度控制為10以內的隨機整數
?*?----每條字符串都是隨機生成的字符,字符可重復
?*?----但字符串不可重復
?*/
public?class?CollectionsDemo?{
?????
public?void?IntCollection(){
List<Integer>?intList?=?new?ArrayList<Integer>();
Integer?tmpInt?;
for(int?i=0;i<10;i++){
do{
Random?randomInt?=new?Random();?
//An?instance?of?this?class?is?used?to?generate?a?stream?of?pseudorandom?numbers.
tmpInt?=?randomInt.nextInt(100);
//返回0-100以內的整數
System.out.println("將要添加整數"+tmpInt);
????}while(intList.contains(tmpInt));
????????intList.add(tmpInt);
?????????}
System.out.println("?");
System.out.println("========排序前===========");
for(Integer?tmpNo?:intList){
System.out.println(tmpNo);
}
System.out.println("?");
System.out.println("========排序后===========");
Collections.sort(intList);
//void方法,Sorts?the?specified?list?into?ascending?order,?according?to?the?natural?ordering?of?its?elements.
for(Integer?tmpNo?:intList){
System.out.println(tmpNo); }
}
public?void?StrCollection(){
List<String>?strList?=?new?ArrayList<String>();
System.out.println("?");
System.out.println("========請輸入3個字符串===========");
// ?quantity?=input.nextInt();}?//Scans?the?next?token?of?the?input?as?an?int.
for(int?i=1;i<=3;i++){
System.out.println("請輸入第"+i+"個字符串");
Scanner?input?=?new?Scanner(System.in);
?String?inputStr?=input.next();
if(strList.contains(inputStr)){?//如果為true
System.out.println("字符串"+inputStr+"已存在,請重新輸入");
strList.remove(inputStr);
i--;
}
?strList.add(inputStr);
}
System.out.println("?");
System.out.println("========排序前===========");
for(String?tmpName?:strList){
System.out.println(tmpName);
}
System.out.println("?");
System.out.println("========排序后===========");
Collections.sort(strList);
//void返回值的方法,Sorts?the?specified?list?into?ascending?order,?according?to?the?natural?ordering?of?its?elements.
for(String?tmpName?:strList){
System.out.println(tmpName); }
}
//生成字符表,借鑒一位慕課網童鞋的,對字符方面玩得真的很溜??!佩服
public?void?generateAlphabeta(List<Character>?list,char?st,char?ed){
?for(char?c=?st;c<=ed;c++){
?list.add(c);
}
?}
public?void?StrCollectionDemo(){
List<String>?stringList?=?new?ArrayList<String>();
List<Character>?subList?=?new?ArrayList<Character>();
???????
generateAlphabeta(subList,?'a',?'z');//將a-z加入字符表
generateAlphabeta(subList,?'A',?'Z');//將A-Z加入字符表
generateAlphabeta(subList,?'0',?'9');//將0-9加入字符表
Random?number?=?new?Random();
int?length;?????????????????????
for(int?i=0;i<10;i++){
do{
length?=number.nextInt(11);//規定1-10?的長度,返回一個1-10的其中一個數
}while(length?==0);
String?tmpStr?="";
for(int?y=0;y<length;y++){
Random?index?=new?Random();
tmpStr?=tmpStr+subList.get(index.nextInt(subList.size()));
}
stringList.add(tmpStr);
System.out.println("添加"+stringList.get(i)+"元素成功");
}
System.out.println("?");
System.out.println("========排序前===========");
for(String?tmpString?:stringList){
System.out.println(tmpString);
}
System.out.println("?");
System.out.println("========排序后===========");
Collections.sort(stringList);
//void返回值的方法,Sorts?the?specified?list?into?ascending?order,?according?to?the?natural?ordering?of?its?elements.
for(String?tmpString??:stringList){
System.out.println(tmpString?); }
}
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
CollectionsDemo?t01?=new?CollectionsDemo(?);
t01.IntCollection();
//t01.StrCollection();
????t01.StrCollectionDemo();
}
}
/*
?*?運用Collections.sort(List?list)對集合中的元素進行排序
?*?public?class?Collections?extends?Object
?*?This?class?consists?exclusively?of?static?methods?that?operate?on?or?return?collections.?
?*?①對Integer類的list集進行排序
?*?----10個隨機整數,打印出排序前和排序后
?*?
?*?②對String類的list集進行排序
?*?----人手輸入3個字符串,并打印出排序前和排序后
?*?
?*?③作業,存儲隨機生成的String類的list集并對其進行排序
?*?----創建List<String>?str集合后,往其添加十條隨機字符串
?*?----每條字符串的長度控制為10以內的隨機整數
?*?----每條字符串都是隨機生成的字符,字符可重復
?*?----但字符串不可重復
?*/
public?class?CollectionsDemo?{
?????
public?void?IntCollection(){
List<Integer>?intList?=?new?ArrayList<Integer>();
Integer?tmpInt?;
for(int?i=0;i<10;i++){
do{
Random?randomInt?=new?Random();?
//An?instance?of?this?class?is?used?to?generate?a?stream?of?pseudorandom?numbers.
tmpInt?=?randomInt.nextInt(100);
//返回0-100以內的整數
System.out.println("將要添加整數"+tmpInt);
????}while(intList.contains(tmpInt));
????????intList.add(tmpInt);
?????????}
System.out.println("?");
System.out.println("========排序前===========");
for(Integer?tmpNo?:intList){
System.out.println(tmpNo);
}
System.out.println("?");
System.out.println("========排序后===========");
Collections.sort(intList);
//void方法,Sorts?the?specified?list?into?ascending?order,?according?to?the?natural?ordering?of?its?elements.
for(Integer?tmpNo?:intList){
System.out.println(tmpNo); }
}
public?void?StrCollection(){
List<String>?strList?=?new?ArrayList<String>();
System.out.println("?");
System.out.println("========請輸入3個字符串===========");
// ?quantity?=input.nextInt();}?//Scans?the?next?token?of?the?input?as?an?int.
for(int?i=1;i<=3;i++){
System.out.println("請輸入第"+i+"個字符串");
Scanner?input?=?new?Scanner(System.in);
?String?inputStr?=input.next();
if(strList.contains(inputStr)){?//如果為true
System.out.println("字符串"+inputStr+"已存在,請重新輸入");
strList.remove(inputStr);
i--;
}
?strList.add(inputStr);
}
System.out.println("?");
System.out.println("========排序前===========");
for(String?tmpName?:strList){
System.out.println(tmpName);
}
System.out.println("?");
System.out.println("========排序后===========");
Collections.sort(strList);
//void返回值的方法,Sorts?the?specified?list?into?ascending?order,?according?to?the?natural?ordering?of?its?elements.
for(String?tmpName?:strList){
System.out.println(tmpName); }
}
//生成字符表,借鑒一位慕課網童鞋的,對字符方面玩得真的很溜??!佩服
public?void?generateAlphabeta(List<Character>?list,char?st,char?ed){
?for(char?c=?st;c<=ed;c++){
?list.add(c);
}
?}
public?void?StrCollectionDemo(){
List<String>?stringList?=?new?ArrayList<String>();
List<Character>?subList?=?new?ArrayList<Character>();
???????
generateAlphabeta(subList,?'a',?'z');//將a-z加入字符表
generateAlphabeta(subList,?'A',?'Z');//將A-Z加入字符表
generateAlphabeta(subList,?'0',?'9');//將0-9加入字符表
Random?number?=?new?Random();
int?length;?????????????????????
for(int?i=0;i<10;i++){
do{
length?=number.nextInt(11);//規定1-10?的長度,返回一個1-10的其中一個數
}while(length?==0);
String?tmpStr?="";
for(int?y=0;y<length;y++){
Random?index?=new?Random();
tmpStr?=tmpStr+subList.get(index.nextInt(subList.size()));
}
stringList.add(tmpStr);
System.out.println("添加"+stringList.get(i)+"元素成功");
}
System.out.println("?");
System.out.println("========排序前===========");
for(String?tmpString?:stringList){
System.out.println(tmpString);
}
System.out.println("?");
System.out.println("========排序后===========");
Collections.sort(stringList);
//void返回值的方法,Sorts?the?specified?list?into?ascending?order,?according?to?the?natural?ordering?of?its?elements.
for(String?tmpString??:stringList){
System.out.println(tmpString?); }
}
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
CollectionsDemo?t01?=new?CollectionsDemo(?);
t01.IntCollection();
//t01.StrCollection();
????t01.StrCollectionDemo();
}
}
////生成字符表
//public?void?generatorAlphabeta(List<Character>?list,char?st,char?ed){
//??????for(char?c=?st;c<=ed;c++){
//??????????list.add(c);
//??????}
//??}
//???
//public?void?testSort2(){
//??????List<String>?stringList?=?new?ArrayList<String>();
//??????List<Character>?subList?=?new?ArrayList<Character>();
//???????
//??????generatorAlphabeta(subList,?'a',?'z');//將a-z加入字符表
//??????generatorAlphabeta(subList,?'A',?'Z');//將A-Z加入字符表
//??????generatorAlphabeta(subList,?'0',?'9');//將0-9加入字符表
2016-02-01
import?java.util.stream.IntStream;
請問這是干嘛用的