Java 随机化字符串数组的排序
我是Android新手,我正在寻找一种最好、最快的方法来随机化string[]数组的排名,同时跟踪其中的一个字符串 数组通常只有5个字符串长,例如Java 随机化字符串数组的排序,java,android,arrays,string,Java,Android,Arrays,String,我是Android新手,我正在寻找一种最好、最快的方法来随机化string[]数组的排名,同时跟踪其中的一个字符串 数组通常只有5个字符串长,例如 a) input String[] All = {"apple", "fish", "cat, "dog", "mouse"} b) output (one of many) String[] All = {"fish", "dog", "cat, "apple", "mo
a) input
String[] All = {"apple", "fish", "cat, "dog", "mouse"}
b) output (one of many)
String[] All = {"fish", "dog", "cat, "apple", "mouse"}
问题是,目标字符串将是“apple”,需要在其随机化后进行跟踪。换句话说,我想知道苹果在哪里(就上面的输出而言,苹果是所有[3])
如何执行此操作?您可以使用
Collections.shuffle()
,然后将字符串数组转换为List
,以便使用Collections类的shuffle方法。之后,迭代字符串列表以查找apple的索引
示例:
String[] All = {"apple", "fish", "cat","dog", "mouse"};
List<String> list = Arrays.asList(All);
Collections.shuffle(list);
System.out.println(list.toString());
for(int i = 0; i < list.size(); i++)
{
if(list.get(i).equals("apple"))
{
System.out.println("Apple at index: " + i);
break;
}
}
[fish, apple, dog, cat, mouse]
Apple at index: 1
谢谢你的帮助,但据我所知,“list”是int?哪个是[fish,apple,dog,cat,mouse]的洗牌字符串[]数组?