java - 在Java中,如何创建n个具有某些属性的字符串

标签 java string random

我想做的是生成 n 个字符串,每个字符串应该有 2 个字符和 4 个随机打乱的数字,例如: 2w3e45 34rt56 qw5498 0126fd w5487t

等等,我有以下内容,它正在工作,可以应用任何增强功能吗? 另外,我们可以将它们作为唯一的字符串吗?

import java.util.Random;

public class RandomNumAndChar {

public static void main(String[] args) {

    for (int j = 0; j < 10; j++) {
        Random r = new Random();
        Integer[] rn = new Integer[4];
        String numbers = new String();
        for (int i = 0; i < rn.length; i++) {
            rn[i] = r.nextInt(10);
            numbers = numbers + rn[i].toString();
        }
        char c = (char) (r.nextInt(26) + 'a');
        char c2 = (char) (r.nextInt(26) + 'a');
        String chars = "" + c + c2;
        String fin = numbers.concat(chars);
        System.out.println(RandomNumAndChar.shuffle(fin));


    }


}

public static String shuffle(String s) {
    char[] characters = s.toCharArray();
    for (int i = 0; i < characters.length; i++) {
        int randomIndex = (int)(Math.random() * characters.length);
        char temp = characters[i];
        characters[i] = characters[randomIndex];
        characters[randomIndex] = temp;
    }
    return new String(characters);
}

}

最佳答案

正如评论中所述,这更适合 CodeReview Stack Exchange 。不过,这里有一些我将用来执行任务的指南:

  • 使用 6 个字符 数组来存储代码。请注意,我说的是Charact,而不是char。这是因为 Java 在处理包装类时更加灵活。
  • 使用 Collections#shuffle() 来打乱数组。这就是 Character 发挥作用的地方,因为我们需要使用 Arrays#asList() 转换 List 中的数组。
  • 使用一种方法来生成整个代码,而不仅仅是用于改组。

使用这个简单的指南,您会得到类似的结果:

class Test {

    static Random r = new Random();

    public static void main(String[] a) {

        for (int i = 0; i < 10; i++) {
            System.out.println(getNewCode());
        }
    }

    private static String getNewCode() {
        Character[] array = new Character[6];

        int i = 0;
        while(i < 4) array[i++] = (char) ('0' + r.nextInt(10));

        array[4] = (char) ('a' + r.nextInt(26));
        array[5] = (char) ('a' + r.nextInt(26));

        List<Character> l = Arrays.asList(array);

        Collections.shuffle(l);

        StringBuilder sb = new StringBuilder();

        for (char s : l)
            sb.append(s);

        return sb.toString();
    }
}

示例输出:

e1v456
8s632b
t7810w
n9801y
76q14c
p2a881
27m7k1
1081st
m19u36
t03h77

更方便的是,您可以直接使用 List 而不是数组开始:

class Test {

    static Random r = new Random();

    public static void main(String[] a) {

        for (int i = 0; i < 10; i++) {
            System.out.println(getNewCode());
        }
    }

    private static String getNewCode() {
        List<Character> l = new ArrayList<Character>();

        int i = 0;
        for(int i = 0; i<4; i++)
            l.add((char) ('0' + r.nextInt(10)));

        l.add((char) ('a' + r.nextInt(26)));
        l.add((char) ('a' + r.nextInt(26)));

        Collections.shuffle(l);

        StringBuilder sb = new StringBuilder();

        for (char s : l)
            sb.append(s);

        return sb.toString();
    }
}

关于java - 在Java中,如何创建n个具有某些属性的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28881844/

相关文章:

c++ - 加权随机数

java - 实现这个最有效的方法是什么?

java - AWS Neptune io.netty.handler.codec.CorruptedFrameException

string - 如何仅在分隔符的第一个实例之后拆分字符串?

c# - 创建空数组并添加值导致 "Array index is out of range"

string - 如果我知道所有句子的发音,如何获得句子中汉字的发音?

c# - 随机化字符串中的字符列表,C#

c - 如何存储(scanf)随机生成的数字? (里面有代码)

java - 使用两种编程语言生成UUID?

java - 根据java中的最新时间戳从列表中查找唯一记录