# Compare 2 IntList without 'for'?

**URL:** <https://discourse.processing.org/t/compare-2-intlist-without-for/7035>\
**Category:** Coding Questions\
**Created:** [December 30, 2018, 11:42pm UTC](https://discourse.processing.org/t/compare-2-intlist-without-for/7035 "2018-12-30T23:42:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![amaia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/amaia/32/3012_2.png) [@amaia](https://discourse.processing.org/u/amaia)\
**Post date:** [December 30, 2018, 11:42pm UTC](https://discourse.processing.org/t/compare-2-intlist-without-for/7035/1 "2018-12-30T23:42:50Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [December 31, 2018, 12:02am UTC](https://discourse.processing.org/t/compare-2-intlist-without-for/7035/2 "2018-12-31T00:02:58Z")

</div>

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 `String`s instead of just `==`, because `String`s 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)… 😃

---

<div class="post-metadata">

**Author:** ![amaia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/amaia/32/3012_2.png) [@amaia](https://discourse.processing.org/u/amaia)\
**Post date:** [December 31, 2018, 1:15am UTC](https://discourse.processing.org/t/compare-2-intlist-without-for/7035/3 "2018-12-31T01:15:20Z")

</div>

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

```auto

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…?

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [December 31, 2018, 1:31am UTC](https://discourse.processing.org/t/compare-2-intlist-without-for/7035/4 "2018-12-31T01:31:44Z")

</div>

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:

```auto
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;
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 31, 2018, 2:17am UTC](https://discourse.processing.org/t/compare-2-intlist-without-for/7035/5 "2018-12-31T02:17:50Z")

</div>

> [@Architector\_4](#):
>
> if(a.get(i)!=b.get(i)) return false;

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:

```auto
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:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [December 31, 2018, 2:19am UTC](https://discourse.processing.org/t/compare-2-intlist-without-for/7035/6 "2018-12-31T02:19:49Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![amaia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/amaia/32/3012_2.png) [@amaia](https://discourse.processing.org/u/amaia)\
**Post date:** [December 31, 2018, 4:00am UTC](https://discourse.processing.org/t/compare-2-intlist-without-for/7035/7 "2018-12-31T04:00:30Z")

</div>

```auto
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.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [January 15, 2019, 12:37am UTC](https://discourse.processing.org/t/compare-2-intlist-without-for/7035/8 "2019-01-15T00:37:11Z")

</div>

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](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](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](https://stackoverflow.com/a/11888735/7207622)

```auto
if(set1.equals(set2)){
  println("equal!");
}

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [January 15, 2019, 4:54pm UTC](https://discourse.processing.org/t/compare-2-intlist-without-for/7035/9 "2019-01-15T16:54:25Z")

</div>

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.
