为了账号安全,请及时绑定邮箱和手机立即绑定

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

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入门第三季
  • 参与学习       409792    人
  • 解答问题       4340    个

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

进入课程

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

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信