手记

关于如何随机产生字符串的问题

package com.course;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

public class CollectionTest {

List<String>StringList=new ArrayList<String>();
Random random=new Random();

public void testAdd(){
    StringBuilder sb=new StringBuilder();
    String base = "abcdefghijklmnopqrstuvwxyz0123456789"; 
    int length =random.nextInt(10)+1;   //生成长度为10以内的字符串

        for (int i = 0; i <length; i++) {
            int num=random.nextInt(base.length());
            sb.append(base.charAt(num));//添加字符

        }
        System.out.println(sb);
    }

    public void testSort1(){
        for (int j = 0; j < 10; j++) {
            System.out.print(j+":");
            testAdd();
        }

    }       
public static void main(String[] args) {
    CollectionTest ct=new CollectionTest();
    ct. testSort1();
}

}

1人推荐
随时随地看视频
慕课网APP