Repeating and randomizing string arrays

The shuffle() function from the 1st link I’ve posted is for arrays of datatype int only:

@SafeVarargs final int[] shuffle(final int... arr) {
  if (arr == null)  return null;
 
  int idx = arr.length;
 
  while (idx > 1) { 
    final int rnd = (int) random(idx--), tmp = arr[idx];
    arr[idx] = arr[rnd];
    arr[rnd] = tmp;
  }
 
  return arr;
}

For object-typed arrays, we need to convert it, like this:

@SafeVarargs final <T> T[] shuffle(final T... arr) {
  if (arr == null)  return null;

  int idx = arr.length;

  while (idx > 1) { 
    final int rnd = (int) random(idx--);
    final T tmp = arr[idx];

    arr[idx] = arr[rnd];
    arr[rnd] = tmp;
  }

  return arr;
}

Alternatively, as the 2nd posted link advises, we can use the not-so-new Processing containers.

For strings, we use a StringList: StringList / Reference / Processing.org

So we can have access to its methods StringList::shuffle() & StringList::array():

  1. shuffle() / Reference / Processing.org
  2. array() / Reference / Processing.org
2 Likes