0

I am trying to check if a palindrome using java's arraylist, and though my logic makes sense the output doesn't. If I use the Collections.reverse method to reverse the arraylist it gives me true for a number that's not a palindrome.

        int temp = 123;
        ArrayList<Integer> num = new ArrayList<Integer>();
        
        while(temp!=0){
            
            num.add(temp%10);
            temp=temp/10;
            
        }

        ArrayList<Integer> ognum = num;
        Collections.reverse( ognum);

        if(num.equals(ognum)){
            return true;
        }else{
            return false;
        }

Output: true

However if I do this:

        int temp = 123;
        ArrayList<Integer> num = new ArrayList<Integer>();
        
        while(temp!=0){
            
            num.add(temp%10);
            temp=temp/10;
            
        }

        ArrayList<Integer> ognum = new ArrayList<Integer>();
        ognum.add(1);
        ognum.add(2);
        ognum.add(3);

        if(num.equals(ognum)){
            return true;
        }else{
            return false;
        }

Output: false

  • 5
    `ArrayList ognum = num;` what do you think this does? – Federico klez Culloca Aug 26 '22 at 19:20
  • Try printing both `num` and `ognum` after reversing ognum. Especially in the first case. As @FedericoklezCulloca already hinted, you are not creating another list in the first case, just another reference to it. – H3AR7B3A7 Aug 26 '22 at 19:29
  • 2
    `ognum` and `num` are the same list (or better, they point to the same list instance) - BTW you can use `new ArrayList<>(num)` to create a new list with the same content as `num` (a copy) || `return num.equals(ognum)` is more *elegant*, avoiding the un-needed `if` (*if it is true return true else false*) – user16320675 Aug 26 '22 at 19:37
  • 2
    Try this instead. `List ognum = new ArrayList<>(num);` – WJS Aug 26 '22 at 19:38
  • Does this answer your question? [How do I copy an object in Java?](https://stackoverflow.com/questions/869033/how-do-i-copy-an-object-in-java) – Progman Aug 26 '22 at 21:20

1 Answers1

1

The culprit is the ArrayList<Integer> ognum = num; instruction.

Instead of copying the values inside the ArrayList, all you're doing is copying the same reference into another variable, thus making them the same thing.

If you're new to OOP, what you're essentially doing is grabbing a box, putting a label "num" on it and add some stuff into it. After some time, you grab the very same box and you add a second label, this time "ognum". No matter what you do with the content of the box, what is inside the box "num" it will be the same as the box "ognum".

This is how your code should look like:

    int temp = 123;
    ArrayList<Integer> num = new ArrayList<Integer>();
        
    while(temp!=0){
            
        num.add(temp%10);
        temp=temp/10;
            
     }
     // Here num will contain the values [3,2,1]
    
     ArrayList<Integer> ognum = new ArrayList<>();
     ognum.addAll(num);
     // Here ognum will contain the values [3,2,1]
     
     Collections.reverse(ognum);
     // Here ognum will contain the values [1,2,3]

     return num.equals(ognum); // [3,2,1] == [1,2,3] ? => false