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
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
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
Get step-by-step solutions from verified subject matter experts
