0

I'm new to Android and I'm looking for the best and quickest way to randomize the ranking of a string[] array while tracking one of the strings therein.

The array is typically just 5 strings long e.g.

         a) input
          String[] All = {"apple", "fish", "cat, "dog", "mouse"}

         b) output (one of many)
          String[] All = {"fish", "dog", "cat, "apple", "mouse"}

The thing is that the target string would be 'apple' that needs to be tracked after its randomized. In other words, I want to know where the apple is (in the case of output above, apple is All[3])

How do I go about doing this?

Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
user3492802
  • 179
  • 9

1 Answers1

0

You can use the Collections.shuffle() and then convert your array of String to List<String> so you can use the shuffle method of the Collections class. After, you then iterate the List of string to find the index of apple.

sample:

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;
    }
}

result:

[fish, apple, dog, cat, mouse]
Apple at index: 1
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • Thanks for your help, but from what little I understand, 'list' is an int? Which is the shuffled string[] array for [fish, apple, dog, cat, mouse]? – user3492802 Jun 23 '14 at 13:54