Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中,如何以相同的顺序洗牌两个数组_Java_Arrays_List_Collections - Fatal编程技术网

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

在java中,如何以相同的顺序洗牌两个数组,java,arrays,list,collections,Java,Arrays,List,Collections,我有两组问题和答案 String questions[] = { "Q1?", "Q2?", "Q3?"}; String answers[] = { "A1?", "A2?", "A3?"}; 我使用了Collections.shuffle(Arrays.asList(questions);来洗牌每个数组。如何洗牌每个数组,使它们在洗牌后保持相同的顺序?您可以洗牌一个包含索引的新数组。然后从第一个索引中从两个数组中获取元素 List<Integer> i

我有两组问题和答案

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

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

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

您可以洗牌一个包含索引的新数组。然后从第一个索引中从两个数组中获取元素

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

Collections.shuffle(indexArray);

String question = questions[indexArray.get(0)];
String answer = answers[indexArray.get(0)];
List indexArray=Arrays.asList(0,1,2);
收藏。洗牌(索引);
字符串问题=问题[indexArray.get(0)];
字符串答案=答案[indexArray.get(0)];


当然,正如其他答案所建议的那样,创建一个包含
问题
答案
类将是一种更为糟糕的方法。这样,与当前方法中的
3个数组
相比,您只需维护一个
列表
数组

创建一个类
问题和答案
并使用该类的数组。

创建一个将问题和答案放在一起的类将是一个更简单、更面向对象的解决方案:

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

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

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

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

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

Java Collections有一个(令人惊讶的)简单的解决方案:
Collections.shuffle(Collection,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);
最初发布于:

创意来源:

publicstaticvoidshuffle2arraystogher(字符串[]a,字符串[]b){
如果(a.length==b.length){
int n=a.长度;
随机=新随机();
random.nextInt();
对于(int i=0;i
从面向对象的角度来看,链接的可能重复提出了一种更好的方法,即将问题和答案存储在一起。然后,您只有一个数组或列表,可以轻松地进行无序排列。虽然将两个列表无序排列是一种很好的方法,但它并不能解决数组问题,因为请尝试我的答案,您可以使用
列表indexArray=Arrays.asList(0,1,2);
    long seed = System.nanoTime();
    Random rnd = new Random(seed);
    Collections.shuffle(quests, rnd);
    Collections.shuffle(answers, rnd);
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);
}