【问题标题】:How to get 10 random strings for a string array?如何为字符串数组获取 10 个随机字符串?
【发布时间】:2017-01-01 18:08:39
【问题描述】:

大家好,我想知道如何创建一个从 java 数组中的一组字符串(大约 96 个字符串)中随机获取 10 个字符串的方法...

字符串取自文本文件 我已经创建了一种方法来读取文件并将变量分配给行 我还创建了一系列问题

这是我读取文件和创建问题数组的代码

q 是问题

选择一个

b ....

.

.

cA 是正确答案

public static Question[] readAllQuestions() throws FileNotFoundException {
    int numberOfQuestions = CountQuestions();
    Question [] allQuestions = new Question[numberOfQuestions];
    Scanner file = new Scanner (new File("TestBank.txt"));
    String q = "";
    String aa = "";
    String bb = "";
    String cc = "";
    String dd = "";
    String ccA = "";
    int x = 0 ; 
    int k = 0 ; 
    while (x< (allQuestions.length-1)) {
        while (file.hasNext() && k == 0) {
            String ques[] = file.nextLine().split(" ");
            for (int ww = 0 ; ww< ques.length ; ww++) {
                q += ques[ww];
                q+=" ";
            }
            if(ques[0].equals("")) {
                k++ ; 
            }
        }
        while (k == 1) {
            String a[] = file.nextLine().split(" ");
            for (int ww = 0; ww< a.length; ww++) {
                aa += a[ww];
                aa+= " ";
            }
            if(a[0].equals("")) {
                k++ ;
            }
        }
        //file.hasNext() &&
        while (k == 2) {
            String b[] = file.nextLine().split(" ");
            for (int ww = 0 ; ww< b.length ; ww++) {
                bb += b[ww];
                bb+= " ";
            }
            if(b[0].equals("")) {
                k++;
            }
        }
        while (k == 3) {
            String c[] = file.nextLine().split(" ");
            for (int ww = 0; ww<c.length; ww++) {
                cc += c[ww];
                cc+= " ";
            }
            if(c[0].equals("")) {
                k++;
            }
        }
        while (k == 4) {
            String d[] = file.nextLine().split(" ");
            for (int ww = 0; ww< d.length; ww++) {
                dd += d[ww];
                dd+= " ";
            }
            if(d[0].equals("")) {
                k++;
            }
        }
        while (k == 5) {
            String cA[] = file.nextLine().split(" ");
            for (int ww = 0; ww<cA.length; ww++) {
                ccA += cA[ww];
                ccA += " ";
            }
            if(cA[0].equals("")) {
                k++;
            }
        }
        while (k == 6) {
            Question question = new Question(q,aa,bb,cc,dd,ccA);
            allQuestions[x] = question;
            q="";
            aa="";
            bb="";
            cc="";
            dd="";
            ccA="";
            x++;
            k=0;
        }
    } 
    return allQuestions;
} 

有什么想法吗? 谢谢

【问题讨论】:

    标签: java arrays string java.util.scanner


    【解决方案1】:

    在我看来,您想要从字符串数组中随机抽取 10 个字符串而不选择重复项。实现此目的的一种简单方法是使用Collections::shuffle 随机排列元素,然后选择打乱列表的子集。这是一个例子:

    public static void main(String[] args) throws Exception
    {
        String[] exStringArray = IntStream.range(0, 96)
                .mapToObj(Integer::toString)
                .toArray(size -> new String[size]);
    
        System.out.println(getRandomElements(Arrays.asList(exStringArray), 10));
    }
    
    private static <T> Collection<T> getRandomElements(Collection<T> c, int maxElements)
    {
        List<T> deepCopiedList = new ArrayList<>(c);
        Collections.shuffle(deepCopiedList);
        return Collections.unmodifiableList(deepCopiedList.subList(0, Math.max(Math.min(deepCopiedList.size(), maxElements), 0)));
    }
    

    【讨论】:

    • 覆盖 toString
    【解决方案2】:

    您可以使用 Random 类。 array[random.nextInt(array.length)]给出从0array.length - 1的任意数字索引的数组元素,因为nextInt方法中排除了最后一个索引。

    public static String[] getRandomStrings(String [] mainStrings, int i) {
        String [] randomStrings = new String[i];
        Random random = new Random();
        for(int index = 0; index < i ; index++){
            randomStrings[index] = mainStrings[random.nextInt(mainStrings.length)];
        }
        return randomStrings;
    }
    

    【讨论】:

    • 它有效,但是当我打印数组时它只打印地址而不是数据!
    • 使用循环打印元素。
    猜你喜欢
    • 2021-03-06
    • 2014-02-09
    • 2012-06-09
    • 2019-04-01
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多