如何在java中以相同的顺序混洗两个数组

时间:2021-07-15 14:07:52

I've got two arrays of question and answers

我有两个问题和答案

String questions[] = {
"Q1?",
"Q2?",
"Q3?"};

String answers[] = {
    "A1?",
    "A2?",
    "A3?"};

I used Collections.shuffle(Arrays.asList(questions); to shuffle each arrays. How do I shuffle each array so that after shuffling they maintain same order?

我使用了Collections.shuffle(Arrays.asList(questions);来洗牌每个数组。如何对每个数组进行混洗,以便在洗牌后它们保持相同的顺序?

6 个解决方案

#1


10  

You can rather shuffle a new array which holds the indices. And then get the elements from both array from the first index.

你可以改为一个包含索引的新数组。然后从第一个索引获取两个数组中的元素。

List<Integer> indexArray = Arrays.asList(0, 1, 2);

Collections.shuffle(indexArray);

String question = questions[indexArray.get(0)];
String answer = answers[indexArray.get(0)];

Of course, creating a class containing questions and answers would be a more OO way, as other answers suggest. That way, you would have to maintain just one List or array, as compared to 3 arrays in the current approach.

当然,正如其他答案所暗示的那样,创建一个包含问题和答案的课程将更为OO。这样,与当前方法中的3个数组相比,您只需维护一个List或数组。

#2


7  

Creating a class for holding both the question and answer together would be an easier and more OO solution:

创建一个用于同时保持问题和答案的类将是一个更容易和更OO的解决方案:

class QuestionAnswerPair {
    private final String question;
    private final String answer;

    public QuestionAnswerPair(String question, String answer) {
        this.question = question;
        this.answer = answer;
    }
}

And then:

QuestionAnswerPair[] questions = new QuestionAnswerPair[] {
    // Put questions here
};

Collections.shuffle(Arrays.asList(questions));

#3


6  

Create a class QuestionAndAnswer and use an array of that class.

创建一个类QuestionAndAnswer并使用该类的数组。

#4


1  

Instead of shuffling answers and questions, you may shuffle an extra array of integers that has indexes to questions/answers and then extract question and answers from corresponding arrays using shuffled indexes.

您可以将一个额外的整数数组洗牌,而不是改变答案和问题,这些整数具有问题/答案的索引,然后使用混洗索引从相应的数组中提取问题和答案。

#5


0  

Java Collections has a (surprisingly) simple solution to this problem: Collections.shuffle(Collection<?>, Random) with a Random seeded with same seed.

Java Collections有一个(令人惊讶的)简单的解决方案来解决这个问题:Collections.shuffle(Collection ,Random),其中Random使用相同的种子播种。

    List<Integer> quests = Arrays.asList(1, 2, 3, 4, 5);
    List<Integer> answers = Arrays.asList(10, 20, 30, 40, 50);

    long seed = System.nanoTime();
    Collections.shuffle(quests, new Random(seed));
    Collections.shuffle(answers, new Random(seed));

    System.out.println(quests);
    System.out.println(answers);

Note:

Extra optimization is dangerous. This DOE NOT WORK:

额外的优化是危险的。这个DOE不工作:

    long seed = System.nanoTime();
    Random rnd = new Random(seed);
    Collections.shuffle(quests, rnd);
    Collections.shuffle(answers, rnd);

Originally posted at: https://*.com/a/44863528/1506477

最初发布于:https://*.com/a/44863528/1506477

#6


0  

Idea from: http://www.vogella.com/tutorials/JavaAlgorithmsShuffle/article.html

来自:http://www.vogella.com/tutorials/JavaAlgorithmsShuffle/article.html

public static void shuffle2ArraysTogther(String[] a, String[] b) {
    if(a.length == b.length) {
        int n = a.length;
        Random random = new Random();
        random.nextInt();
        for (int i = 0; i < n; i++) {
            int change = i + random.nextInt(n - i);
            swap(a, i, change);
            swap(b, i, change);
        }
    }
}

private static void swap(String[] a, int i, int change) {
    String helper = a[i];
    a[i] = a[change];
    a[change] = helper;
}

private static void swap(String[] a, int i, int change) {
    String helper = a[i];
    a[i] = a[change];
    a[change] = helper;
}
String questions[] = {
    "Q1?",
    "Q2?",
    "Q3?"
};

String answers[] = {
    "A1?",
    "A2?",
    "A3?"
};
shuffle2ArraysTogther(questions, answers);
for (String i : questions) {
    System.out.println(i);
}
for (String i : answers) {
    System.out.println(i);
}

#1


10  

You can rather shuffle a new array which holds the indices. And then get the elements from both array from the first index.

你可以改为一个包含索引的新数组。然后从第一个索引获取两个数组中的元素。

List<Integer> indexArray = Arrays.asList(0, 1, 2);

Collections.shuffle(indexArray);

String question = questions[indexArray.get(0)];
String answer = answers[indexArray.get(0)];

Of course, creating a class containing questions and answers would be a more OO way, as other answers suggest. That way, you would have to maintain just one List or array, as compared to 3 arrays in the current approach.

当然,正如其他答案所暗示的那样,创建一个包含问题和答案的课程将更为OO。这样,与当前方法中的3个数组相比,您只需维护一个List或数组。

#2


7  

Creating a class for holding both the question and answer together would be an easier and more OO solution:

创建一个用于同时保持问题和答案的类将是一个更容易和更OO的解决方案:

class QuestionAnswerPair {
    private final String question;
    private final String answer;

    public QuestionAnswerPair(String question, String answer) {
        this.question = question;
        this.answer = answer;
    }
}

And then:

QuestionAnswerPair[] questions = new QuestionAnswerPair[] {
    // Put questions here
};

Collections.shuffle(Arrays.asList(questions));

#3


6  

Create a class QuestionAndAnswer and use an array of that class.

创建一个类QuestionAndAnswer并使用该类的数组。

#4


1  

Instead of shuffling answers and questions, you may shuffle an extra array of integers that has indexes to questions/answers and then extract question and answers from corresponding arrays using shuffled indexes.

您可以将一个额外的整数数组洗牌,而不是改变答案和问题,这些整数具有问题/答案的索引,然后使用混洗索引从相应的数组中提取问题和答案。

#5


0  

Java Collections has a (surprisingly) simple solution to this problem: Collections.shuffle(Collection<?>, Random) with a Random seeded with same seed.

Java Collections有一个(令人惊讶的)简单的解决方案来解决这个问题:Collections.shuffle(Collection ,Random),其中Random使用相同的种子播种。

    List<Integer> quests = Arrays.asList(1, 2, 3, 4, 5);
    List<Integer> answers = Arrays.asList(10, 20, 30, 40, 50);

    long seed = System.nanoTime();
    Collections.shuffle(quests, new Random(seed));
    Collections.shuffle(answers, new Random(seed));

    System.out.println(quests);
    System.out.println(answers);

Note:

Extra optimization is dangerous. This DOE NOT WORK:

额外的优化是危险的。这个DOE不工作:

    long seed = System.nanoTime();
    Random rnd = new Random(seed);
    Collections.shuffle(quests, rnd);
    Collections.shuffle(answers, rnd);

Originally posted at: https://*.com/a/44863528/1506477

最初发布于:https://*.com/a/44863528/1506477

#6


0  

Idea from: http://www.vogella.com/tutorials/JavaAlgorithmsShuffle/article.html

来自:http://www.vogella.com/tutorials/JavaAlgorithmsShuffle/article.html

public static void shuffle2ArraysTogther(String[] a, String[] b) {
    if(a.length == b.length) {
        int n = a.length;
        Random random = new Random();
        random.nextInt();
        for (int i = 0; i < n; i++) {
            int change = i + random.nextInt(n - i);
            swap(a, i, change);
            swap(b, i, change);
        }
    }
}

private static void swap(String[] a, int i, int change) {
    String helper = a[i];
    a[i] = a[change];
    a[change] = helper;
}

private static void swap(String[] a, int i, int change) {
    String helper = a[i];
    a[i] = a[change];
    a[change] = helper;
}
String questions[] = {
    "Q1?",
    "Q2?",
    "Q3?"
};

String answers[] = {
    "A1?",
    "A2?",
    "A3?"
};
shuffle2ArraysTogther(questions, answers);
for (String i : questions) {
    System.out.println(i);
}
for (String i : answers) {
    System.out.println(i);
}