处理随机排列数组

时间:2019-01-23 12:36:42

标签: java arrays processing

我想重新排列数组中的数组,但只有第一个数组

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"}必须保持不变。

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

1 个答案:

答案 0 :(得分:3)

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

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

将显示values是随机重新排列的,而没有修改内部String[]

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