Question: Run the program below (in the examples folder). From the output displayed, you will see that the remove method works differently depending on the base

Run the program below (in the examples folder). From the output displayed, you will see that the remove method works differently depending on the base type of the ArrayList. Why?

import java.util.ArrayList;

class Strange

{

int x = 3;

}

class Main

{

public static void main(String[] args)

{

ArrayList ial = new ArrayList();

Integer i1 = new Integer(1);

Integer i2 = new Integer(1);

if(i1 == i2)

{

System.out.println("i1 == i2 is true");

}

else

System.out.println("I1 == I2 is false");

ial.add(i1);

ial.remove(i1);

System.out.println(ial.size());

ial.add(i1);

ial.remove(i2);

System.out.println(ial.size());

ArrayList ral = new ArrayList();

Strange r1 = new Strange();

Strange r2 = new Strange();

if(r1 == r2)

System.out.println("r1 == r2 is true");

else

System.out.println("r1 == r2 is false");

ral.add(r1);

ral.remove(r1);

System.out.println(ral.size());

ral.add(r1);

ral.remove(r2);

System.out.println(ral.size());

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!