dimanche 7 avril 2019

How to fix list.contains(object) not returning the good boolean variable

I am trying to make a list with some array, generated random, within a range and with a fixed size. The problem is, when I want to see if an array is already in the list (list.contains(array)) it's not returning the good boolean variable. This is how this code looks like, and I have no idea from where's the problem:

List<int[]> list = new ArrayList<int[]>();
int[] v = new int[n];
int n = 2, val_max = 3;

while (list.size() != max) {
  v = getRandomArray(n, 1, val_max);
  if (list.contains(v) == false)
    list.add(v);
}

I expect the output for that to be { {1, 2} {2, 1} {1, 3} {3, 1} {2, 3} {3, 2} } but the output keeps changing... It always doubles one or two of the above values which is what made me think that the problem must be from the contains function.




Aucun commentaire:

Enregistrer un commentaire