Compare 2 IntList without 'for'?

Hello! I want to compare 2 IntList; I’ve tried to compare them without using the for loop, something like this:

IntList ordered;
IntList disordered;

void setup() {
  size(200, 200);
  ordered = new IntList();
  ordered.append(0);
  ordered.append(1);
  ordered.append(2);
  println(ordered);
  disordered = new IntList();
  disordered.append(0);
  disordered.append(1);
  disordered.append(2);
  println(disordered);
}

void draw() {
background(180);
  fill(0);
  if(ordered == disordered){
    text("Yeah, they're the same", 50, 50);
  }else if(ordered != disordered){
    text("No, they're not the same", 50, 50);
  }
}

If you look at println it looks like the code above might work…
I use them as an index in another list: a PImage array where I cut an image like a puzzle and messed it up with disordered.shuffle(); I use this list to move the pieces of the puzzle. When the 2 lists are the same, the puzzle will be finished…(but they’re never the same… ).
Thanks (and sorry my english

1 Like

The problem here is how objects work in Java. IntList is an object, and a variable only contains a pointer to it.

So, IntList ordered; creates a new pointer to an object of type IntList that points to nothing i.e. null by default, and then you do ordered = new IntList();, which creates a new IntList object in memory, and makes ordered to be a pointer to it.

disordered = new IntList(); creates a new IntList object in memory and makes disordered a pointer to it too, but the thing is that ordered and disordered point to 2 unrelated IntList objects.

So, when you do ordered == disordered, you are essentially checking if ordered points to exactly the same IntList object that disordered points to. With your setup, this is always false, because they are pointers to 2 different IntList objects, even if identical.

Your only way here is to use functions to compare the objects themselves instead of just the pointers.
With String (which is also a pointer to an object), this is a.equals(b).

IntList also seems to have .equals(...) function, so I suggest replacing ordered == disordered with ordered.equals(disordered).

By the way, this is also why it’s told to use .equals(...) function on Strings instead of just ==, because Strings are also objects, and variables you use to store them are also only pointers to them.

Yeah, that’s the cool reality of OOP(Object Oriented Programming)… :smiley:

2 Likes

Thank you:smiley: for responding so quickly, and so well explained, even for newbies like me…


IntList ordered;
IntList disordered;

void setup() {
  size(200, 200);
  ordered = new IntList();
  ordered.append(0);
  ordered.append(1);
  ordered.append(2);
  println(ordered);
  disordered = new IntList();
  disordered.append(0);
  disordered.append(1);
  disordered.append(2);
  println(disordered);
}

void draw() {
background(180);
  fill(0);
  compara();
}

void compara(){
  if(disordered.equals(ordered)){
    text("Yeah, they're the same", 50, 50);
  }else if(ordered != disordered){
    text("No, they're not the same", 50, 50);
  }
}

I’ve tried it, but now they are NOT equals:crazy_face:
maybe, I don´t understand it…?

Hmm. Looks like that equals() function is related to something else then. It, too, probably checks if the pointers themselves are equal.

I guess the only way out in this case it to make a function that compares them yourself. Here:

boolean compare(IntList a,IntList b){
  int size=a.size();
  if(size!=b.size()) return false; //They are not the same size.
    for(int i=0;i<size;i++){
      if(a.get(i)!=b.get(i)) return false; //An integer is not equal.
    }
  return true;
}
1 Like

even if the array have same content but are unsorted / differently sorted /
that too might not work

i play with int list and int array,
but if you want use
a.equals(b)
must make a string first:

IntList n1_list;
IntList n2_list;

void setup() {
  size(200, 200);
  n1_list = new IntList();
  n1_list.append(0);
  n1_list.append(1);
  n1_list.append(2);
  println("n1_list "+n1_list);
  n2_list = new IntList();
  n2_list.append(0);
  n2_list.append(2);
  n2_list.append(1);
  println("n2_list "+n2_list);

//  n1_list.sort();
//  n2_list.sort();
//  looks like sort not needed ( see later )

  //int[] n1 = { 0, 1, 2 };
  //int[] n2 = { 0, 2, 1 };
  
  int[] n1 = n1_list.array();
  println("n1: ");  
  println(n1);
  int[] n2 = n1_list.array();
  println("n2: ");  
  println(n2);
  // above printout suggests that the export to array does a sort?? already
  
  String printn1 = "";
  for (int i =0; i<n1.length; i++) printn1 += ","+n1[i];
  println("printn1: "+printn1);
  String printn2 = "";
  for (int i =0; i<n2.length; i++) printn2 += ","+n2[i];
  println("printn2: "+printn2);
  if ( printn1.equals(printn2) ) println("SAME");
}


shows:

n1_list IntList size=3 [ 0, 1, 2 ]
n2_list IntList size=3 [ 0, 2, 1 ]
n1: 
[0] 0
[1] 1
[2] 2
n2: 
[0] 0
[1] 1
[2] 2
printn1: ,0,1,2
printn2: ,0,1,2
SAME
1 Like

Eh. I’m pretty sure that if 2 different IntDict arrays are sorted differently they are different, i.e. not equal, i.e. my function should return false.

Good mention though!

1 Like
compare(a,b);

Works perfectly!!! Thanks a lot Architector_4 and kll
My conclusion:
I need a for loop to compare 2 IntList;
I also think that, chicken with chips is not the same as chips with chicken.

Follow-up:

  1. IntList.equals() is a method “inherited from class java.lang.Object” – that indeed just tests if two IntLists are the same Object. http://processing.github.io/processing-javadocs/core/processing/data/IntList.html

  2. You can iterate and compare contents if you care about order. However if you want to store lists of things and compare their contents in an order-independent way, consider storying them as a Java Set, for example HashSet or TreeSet.

https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html

When you use set, .equals() is defined as “are sets with the same size and elements” – exactly what you want. https://stackoverflow.com/a/11888735/7207622

if(set1.equals(set2)){
  println("equal!");
}
1 Like

I should also mention that using Set (e.g. HashSet or TreeSet) in this way depends on your items (your integers) being unique. It will work for these lists:

1, 2, 3, 4
3, 1, 2, 4

but you can’t store duplicates in a set, only unique items, so an IntList can store:

1, 2, 2, 3, 3, 3
1, 1, 1, 1, 2, 3

but as sets they will become:

1, 2, 3
1, 2, 3

…which means that you can’t use Set to detect a difference between counts of the same entry –

1, 1, 1
1

…for that you need to (for example) loop over an IntList, or use an IntDict or a HashMap.