Basically one function of my app should get an array with different strings from Firestore. Then another function should select 3 different strings from that array and store it in a list. The strings should not be identical, so in the end the list should have 3 unique random strings from the array I got from Firestore.
To achieve this I have the code below (which unfortunately I did not write by myself). Most of the times this code works fine, but sometimes I don't get any value. So as I said sometimes I get this error, which points me to the removeDuplicates
method:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.guessit, PID: 30272
java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.get(ArrayList.java:437)
at com.example.android.guessit.GameFlow.SecondRound.FragmentCategory_2.removeDuplicates(FragmentCategory_2.java:767)
at com.example.android.guessit.GameFlow.SecondRound.FragmentCategory_2.access$100(FragmentCategory_2.java:49)
at com.example.android.guessit.GameFlow.SecondRound.FragmentCategory_2$1.onComplete(FragmentCategory_2.java:99)
at com.google.android.gms.tasks.zzj.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7050)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
Here is the Fragment where I use the function I explained. So basically the getRandomElement
function gets a random string from the array I got from the database and puts it into a list, but before that the removeDuplicates
function checks whether the string is already in the list or not and removes it accordingly:
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
...
btnNavFragCat1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
questionKeyRef.document(mTvCat1).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
List<String> questions = (List<String>) document.get("questions");
List<String> randomList = getRandomElement(questions, 6);
removeDuplicates(randomList);
viewModel.setCategory(mTvCat1);
category.put("category", mTvCat1);
setUpCategory();
setUpQuestions();
}
}
});
}
});
...
private void removeDuplicates(List<String> list) {
int count = list.size();
for (int i = 0; i < count; i++) {
for (int j = i + 1; j < count; j++) {
if (list.get(i).equals(list.get(j))) {
list.remove(j--);
count--;
}
}
}
query1 = list.get(0);
query2 = list.get(1);
query3 = list.get(2);
Log.d("One", list.get(0));
Log.d("Two", list.get(1));
Log.d("Three", list.get(2));
}
private List<String> getRandomElement(List<String> list, int totalItems) {
Random rand = new Random();
List<String> newList = new ArrayList<>();
for (int i = 0; i < totalItems; i++) {
int randomIndex = rand.nextInt(list.size());
newList.add(list.get(randomIndex));
}
return newList;
}
...
I would very much appreciate any help and hope that someone can explain me the error and maybe give me a solution to this.
You should put something to avoid IndexOutOfBoundException
in your RemoveDuplicates
:
private void removeDuplicates(List<String> list) {
int count = list.size();
for (int i = 0; i < count; i++) {
for (int j = i + 1; j < count; j++) {
if (list.get(i).equals(list.get(j))) {
list.remove(j--);
count--;
}
}
}
query1 = list.get(0);
query2 = list.get(1); //This
query3 = list.get(2); //And this, could be out of bound, for example if the list is something like {"a","a","a","a"}, so without duplicates it becomes {"a"}
Log.d("One", list.get(0));
Log.d("Two", list.get(1));
Log.d("Three", list.get(2));
}
So I advice to change it so:
private void removeDuplicates(List<String> list) {
int count = list.size();
for (int i = 0; i < count; i++) {
for (int j = i + 1; j < count; j++) {
if (list.get(i).equals(list.get(j))) {
list.remove(j--);
count--;
}
}
}
count = list.size();
if (count >= 3) {
query1 = list.get(0);
query2 = list.get(1);
query3 = list.get(2);
}
else if (count >= 2) {
query1 = list.get(0);
query2 = list.get(1);
query3 = "";
}
else if (count >= 1) {
query1 = list.get(0);
query2 = "";
query3 = "";
}
else {
query1 = "";
query2 = "";
query3 = "";
}
Log.d("One", query1);
Log.d("Two", query2);
Log.d("Three", query3);
}