Answer the question
In order to leave comments, you need to log in
How to match an element from one array to an element of another array?
There are several phrases. When the button was pressed, it initially generated a random number in a given range equal to the number of phrases, and assigned its own text to each number. But I need no repetition. I found Google, many solutions from SO to generate random numbers without repetitions did not help me, without proper experience I could not cope with them.
As a result, I liked this option more, using JavaScript as an example , and created an array of type String with my own phrases.
I began to use the code from the example on one site:
// Implementing Fisher–Yates shuffle
static void shuffleArray(int[] ar) {
Random rnd = new Random();
for (int i = ar.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
// создадим массив и перемешаем его
int[] mSolutionArray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14 };
shuffleArray(mSolutionArray);
Log.i("Array", Arrays.toString(mSolutionArray));
int mSolutionArray[] = { 0, 1, 2, 3 };
String rnd[] = {
getResources().getString(R.string.random0),
getResources().getString(R.string.random1),
getResources().getString(R.string.random2),
getResources().getString(R.string.random3),
};
Answer the question
In order to leave comments, you need to log in
Shuffles only an array of type int.
static void shuffleArray(String[] ar) {
Random rnd = new Random();
for (int i = ar.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
String a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question