gpt4 book ai didi

java - 如何打乱字符串数组

转载 作者:行者123 更新时间:2023-12-02 08:29:43 25 4
gpt4 key购买 nike

如何在数组下面随机播放

String[] firstName =["a","b","c","d","e"];
String[] lastName =["p","q","r","s","t"];
String[] salary =["10","20","30","40",50"];
String[] phoneNo= ["1","2","3","4","5"];

对数组进行洗牌后,我需要这样的结果

String[] firstName =["d","b","e","c","a"];
String[] lastName =["s","q","t","r","p"];
String[] salary =["40","20","50","30",10"];
String[] phoneNo= ["4","2","5","3","1"];

表示例如索引为 "a"来自firstName从0变化到4,各自索引为"p" , "10" , "1"从 0 更改为 4..

最佳答案

如果您的任务不是执行 snuffle 算法,您可以使用标准 java.util.Collections#shuffle方法:

String[] firstName = new String[] {"a","b","c","d","e"};
List<String> strList = Arrays.asList(firstName);
Collections.shuffle(strList);
firstName = strList.toArray(new String[strList.size()]);

关于java - 如何打乱字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35812232/

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