作业作业,感觉自己写的一般

来源:6-5 应用 Collections.sort() 实现 List 排序

qq_慕神2245698

2020-03-04 14:54

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回答

  • 像风一样自由_
    2020-04-02 09:51:44

    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();

    }



Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409792 学习 · 4340 问题

查看课程

相似问题