Why I not am getting desired result while using == to compare elements of two SORTED arraylist

48 views Asked by At

I want Make a new Arraylist of the common element from Two SORTED ARRAYLIST list1 and list2 I use "==" operator and got wrong answer the I used ".equals()" operator and got correct.

MY QUESTION is If == correctly can not check element of ARRAYLIST. Then why my ANSWER LIST is notempty and why are some elements present in it. why it checked correctly up to some elements and then it failed to check them further.

list1 = [1, 2, 3, 7, 8, 9, 10, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 24, 26, 27, 28, 30, 31, 33, 35, 36, 37, 38, 41, 42, 45, 46, 47, 49, 50, 51, 53, 55, 57, 58, 59, 60, 61, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 74, 75, 77, 78, 79, 80, 82, 83, 85, 87, 88, 90, 91, 92, 94, 95, 96, 97, 100, 102, 103, 104, 109, 110, 111, 112, 113, 114, 116, 117, 118, 119, 120, 121, 122, 125, 126, 129, 130, 132, 133, 137, 138, 139, 140, 141, 145, 147, 148, 151, 152, 153, 154, 158, 159, 161, 163, 164, 165, 168, 169, 170, 172, 175, 176, 178, 179, 180, 181, 183, 184, 185, 186, 187, 188, 190, 191, 193, 194, 195, 196, 199, 200, 201, 203, 205, 206, 208, 209, 210, 211, 212]

list2 = [1, 2, 3, 7, 8, 9, 10, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 24, 26, 27, 28, 30, 31, 33, 35, 36, 37, 38, 41, 42, 45, 46, 47, 49, 50, 51, 53, 55, 57, 58, 59, 60, 61, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 74, 75, 77, 78, 79, 80, 82, 83, 85, 87, 88, 90, 91, 92, 94, 95, 96, 97, 100, 102, 103, 104, 109, 110, 111, 112, 113, 114, 116, 117, 118, 119, 120, 121, 122, 125, 126, 129, 130, 132, 133, 137, 138, 139, 140, 141, 145, 147, 148, 151, 152, 153, 154, 158, 159, 161, 163, 164, 165, 168, 169, 170, 172, 175, 176, 178, 179, 180, 181, 183, 184, 185, 186, 187, 188, 190, 191, 193, 194, 195, 196, 199, 200, 201, 203, 205, 206, 208, 209, 210, 211, 212]

list = [] ---> ANSWER LIST

CODE USING ---> "==" operator

 while (i < list1.size() && j < list2.size()) {
            if (list1.get(i)==list2.get(j)) {
                list.add(list1.get(i));
                i++;
                j++;
            } else if (list1.get(i)>list2.get(j)) {
                j++;
            } else {
                i++;
            }
        }
        System.out.println(list);

INCORRECT ANS

Finally got list as list = [1, 2, 3, 7, 8, 9, 10, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 24, 26, 27, 28, 30, 31, 33, 35, 36, 37, 38, 41, 42, 45, 46, 47, 49, 50, 51, 53, 55, 57, 58, 59, 60, 61, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 74, 75, 77, 78, 79, 80, 82, 83, 85, 87, 88, 90, 91, 92, 94, 95, 96, 97, 100, 102, 103, 104, 109, 110, 111, 112, 113, 114, 116, 117, 118, 119, 120, 121, 122, 125, 126]

CODE USING ---> using ".equals()" operator

while (i < list1.size() && j < list2.size()) {
            if (list1.get(i).equals(list2.get(j))) {
                list.add(list1.get(i));
                i++;
                j++;
            } else if (list1.get(i)>list2.get(j)) {
                j++;
            } else {
                i++;
            }
        }
        System.out.println(list);

CORRECT ANS

I got list as list = [1, 2, 3, 7, 8, 9, 10, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 24, 26, 27, 28, 30, 31, 33, 35, 36, 37, 38, 41, 42, 45, 46, 47, 49, 50, 51, 53, 55, 57, 58, 59, 60, 61, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 74, 75, 77, 78, 79, 80, 82, 83, 85, 87, 88, 90, 91, 92, 94, 95, 96, 97, 100, 102, 103, 104, 109, 110, 111, 112, 113, 114, 116, 117, 118, 119, 120, 121, 122, 125, 126, 129, 130, 132, 133, 137, 138, 139, 140, 141, 145, 147, 148, 151, 152, 153, 154, 158, 159, 161, 163, 164, 165, 168, 169, 170, 172, 175, 176, 178, 179, 180, 181, 183, 184, 185, 186, 187, 188, 190, 191, 193, 194, 195, 196, 199, 200, 201, 203, 205, 206, 208, 209, 210, 211, 212]

Note: main question is if "==" is not a suitable operator for this program why I am not getting totally empty list, why am I getting some elements in the list and why the loop got terminated Half the way.

ALSO FOR

CODE USING ---> "==" BUT WITH -1;

   while (i < list1.size() && j < list2.size()) {
            if (list1.get(i)-1==(list2.get(j))-1) {
                list.add(list1.get(i));
                i++;
                j++;
            } else if (list1.get(i)>list2.get(j)) {
                j++;
            } else {
                i++;
            }
        }
        System.out.println(list);
    }

WITH THIS CODE I AM GETTING CORRECT ANSWER

1

There are 1 answers

0
Old Dog Programmer On

A Java ArrayList or other Collection type can hold only Objects. They cannot hold primitives. To get around this limitation, primitive wrapper classes are used. So, ArrayList<int> foo; will produce a compiler error. But, ArrayList<Integer> foo; is good.

Since an Integer is an Object, this line if (list1.get(i)==list2.get(j)) is asking "Are list.get(i) and list2.get(j) the same reference?", just as if (bar == bin) asks if bar and bin are the same reference (i.e., the same Object).

But, with the primitive wrapper classes, there is auto boxing and auto unboxing. When the compiler sees something like int square = list1.get(i) * list1.get(i), list.get(i).intValue() is implicitly called. This allows you to treat the Integer Object as an int in most cases. But, using == or != are not two of them.

But, if (list1.get(i).equals(list2.get(j))) explicitly tells the compiler you want to compare values, not references.

You can't use >, <, <=, or >= with references. For that reason, when the compiler sees if (list1.get(i)>list2.get(j)), auto unboxing is used. Neither are you allowed to use arithmetic on references. So, when the compiler sees if (list1.get(i)-1==(list2.get(j))-1), the -1 causes auto unboxing to be used.