梁XX
2016-01-31 13:02
在课后作业中,在生成字符的集合时有参考其它慕课同学的写法,借鉴了一个字符类型集合的字符添加方法,并灵活使用了参数列表,佩服。
贴出我的代码,有机会让别的童鞋看看,换位想一下,有机会一起交流。
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加入字符表
import java.util.stream.IntStream;
请问这是干嘛用的
Java入门第三季
409792 学习 · 4340 问题
相似问题