问答详情
源自:6-5 应用 Collections.sort() 实现 List 排序

老师,为什么,添加的随机字符中间会有点,怎么去掉

package collection;


import java.util.Random;

import java.util.Scanner;

import java.util.ArrayList;

import java.util.List;


public class randomString {



private static final Exception OutException = new Exception("超出范围");




public static void main(String[] args) {

// TODO Auto-generated method stub

try {

Scanner input = new Scanner(System.in);

System.out.println("请输入数组个数1~29的数字:");

int num = input.nextInt();

List <Integer> size = randSize(num);

List <String> arry = randString(size);

for (int i = 0 ; i < arry.size(); i++) {

System.out.println(arry.get(i));

}

}catch(Exception e) {

System.out.println(e);

}

}



//生产随机长度

public static List<Integer> randSize(int size) throws java.lang.Exception  {

//创建数值类 泛型 Integer

List <Integer> sizeOfStr = new ArrayList<Integer>();

//创建 random 对象 randSize

Random randsize = new Random();

//10以内随机值赋值给sizeOfStr数组,个数为size

int k;

if (size >= 30 || size < 1) {

throw OutException;

}

for (int i = 0 ; i < size ; i++) {

do {

k = randsize.nextInt(30);

}while(sizeOfStr.contains(k)||k==0);

sizeOfStr.add(k);

}

System.out.println(sizeOfStr);

return sizeOfStr;

}

//产生字符串

public static List <String> randString(List randSize ) {

List <String> randStr = new ArrayList <String>();

List <Character> randChr  = new ArrayList <Character>();

Random randChar = new Random();

for(int i = 0 ; i < randSize.size(); i++) {

//每一组赋值

for(int j = 0 ; j < (int)randSize.get(i); j++ ) {

randChr.add((char)(Math.abs(randChar.nextInt())%(127 -32 +1)+32));

}

randStr.add(randChr.toString());

randChr.clear();

}

randStr.add("done");

return randStr;

}


}


提问者:暗能量PK暗物质 2020-08-23 15:03

个回答

  • 暗能量PK暗物质
    2020-08-23 15:31:16

    package collection;


    import java.util.Random;

    import java.util.Scanner;

    import java.util.ArrayList;

    import java.util.Collections;

    import java.util.List;


    public class randomString {



    private static final Exception OutException = new Exception("超出范围");




    public static void main(String[] args) {

    // TODO Auto-generated method stub

    try {

    Scanner input = new Scanner(System.in);

    System.out.println("请输入数组个数1~29的数字:");

    int num = input.nextInt();

    List <Integer> size = randSize(num);

    List <String> arry = randString(size);

    System.out.println("----------排序前------------");

    for (String i : arry) {

    System.out.println("第"+((int)arry.indexOf(i)+1)+"行:"+i);

    }

    System.out.println("--------排序后---------");

    Collections.sort(arry);

    for (String i : arry) {

    System.out.println("第"+((int)arry.indexOf(i)+1)+"行:"+i);

    }

    }catch(Exception e) {

    System.out.println(e);

    }

    }



    //生产随机长度

    public static List<Integer> randSize(int size) throws java.lang.Exception  {

    //创建数值类 泛型 Integer

    List <Integer> sizeOfStr = new ArrayList<Integer>();

    //创建 random 对象 randSize

    Random randsize = new Random();

    //10以内随机值赋值给sizeOfStr数组,个数为size

    int k;

    if (size >= 30 || size < 1) {

    throw OutException;

    }

    for (int i = 0 ; i < size ; i++) {

    do {

    k = randsize.nextInt(30);

    }while(sizeOfStr.contains(k)||k==0);

    sizeOfStr.add(k);

    }

    System.out.println(sizeOfStr);

    return sizeOfStr;

    }

    //产生字符串

    public static List <String> randString(List randSize ) {

    List <String> randStr = new ArrayList <String>();

    Random randChar = new Random();

    for(int i = 0 ; i < randSize.size(); i++) {

    //每一组赋值

    StringBuilder randChr  = new StringBuilder();

    for(int j = 0 ; j < (int)randSize.get(i); j++ ) {

    randChr.append((char)(Math.abs(randChar.nextInt())%(127 -32 +1)+32));

    }

    randStr.add(randChr.toString());

    }

    return randStr;

    }


    }