gpt4 book ai didi

java - 处理数组中的随机数组

转载 作者:行者123 更新时间:2023-12-01 19:43:51 26 4
gpt4 key购买 nike

我想打乱数组中的数组,但只有第一个数组

String[][] kaarten = {  {"G", "T","1"}, 
{"G", "T","2"},
{"G", "T","3"},
//green triangle
{"R", "T","1"},
{"R", "T","2"},
{"R", "T","3"},
//red triangle
{"B", "T","1"},
{"B", "T","2"},
{"B", "T","3"},
//blue triangle
{"G", "O","1"},
{"G", "O","2"},
{"G", "O","3"},
//green Oval
{"R", "T","1"},
{"R", "T","2"},
{"R", "T","3"},
//Red Oval
{"B", "T","1"},
{"B", "T","2"},
{"B", "T","3"},
//Blue Oval
{"G", "S","1"},
{"G", "S","2"},
{"G", "S","3"},
//green Square
{"R", "T","1"},
{"R", "T","2"},
{"R", "T","3"},
//Red Square
{"B", "T","1"},
{"B", "T","2"},
{"B", "T","3"},
//Blue Square
};

因此,{"G", "T", "1"} 需要保持不变。

我希望你能理解我所看到的,这有点难以解释。

最佳答案

最简单的方法是创建一个 String[] 列表并使用 Collections.shuffle() :

List<String[]> values = new ArrayList<>();
Collections.addAll(values, kaarten);
Collections.shuffle(values);
values.stream().map(Arrays::toString).forEach(System.out::println);

将显示被随机重新排列,而不修改内部String[]:

[G, O, 3] 
[R, T, 3]
[G, O, 2]
[R, T, 3]
[R, T, 1]
...

关于java - 处理数组中的随机数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54327453/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com