-3
String arrAnimal[] = {"cat","dog","parrot","fish"};

I need to shuffle each word and set it to TextView, when click button wanna go through each element(see Shuffeled elements).

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

3 Answers3

0

Complete working code,

String[] animals = {"cat","dog","parrot","fish"};
for (int i = 0; i < animals.length; ++i) {
    List<Character> letters = new ArrayList<>(animals[i].length());
    for (char c : animals[i].toCharArray()) {
        letters.add(c);
    }
    Collections.shuffle(letters);
    StringBuilder builder = new StringBuilder();
    for (char c : letters) {
        builder.append(c);
    }
    animals[i] = builder.toString();
}
System.out.println(Arrays.toString(animals));

Outputs: [tca, ogd, roptar, sifh]

EDIT: To print line by line, change the last line to,

for (String s : animals) {
    System.out.println(s);
}
Hindol
  • 2,924
  • 2
  • 28
  • 41
0

Get the random word:

private var currentWord = getRandomWord()

ArrayList to store the data:

val dashboardList: List<String> =
    listOf("den",
        "music",
        "podcast",
        "library",
        "gallary",
        "work")

Creating a separate method to get the random word from the ArrayList:

  private fun getRandomWord(): String {
        val word = allWordsList.random().toCharArray()
        word.shuffle()
        return String(word)
    }
Stephen
  • 9,899
  • 16
  • 90
  • 137
-1

Use Collections.shuffle( )

ArrayList<String> list = new ArrayList<String>();
list.add("cat");
list.add("dog");
list.add("parrot");
list.add("fish");

Collections.shuffle(list);
kfmes
  • 110
  • 4