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
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)…
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;
}
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.
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.
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.
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.