/*
addAll Method is defined as boolean addAll(int index, Collection c)
*/
import java.util.*;
public class addAll {
public static void main(String[] args) {
ArrayList a1 = new ArrayList();
for(int i=1;i<=3;i++)
{
a1.add(i);
}
System.out.println("The size of a1 is " + a1.size());
for(int i=0;i<a1.size();i++)
{
System.out.println("The Value of a1 is " + a1.get(i));
}
ArrayList a2 = new ArrayList();
a2.addAll(0, a1);
/*
Here if you see even after adding the collection element a1 to addAll method of ArrayList a2
the size and value of a2 collection element remains the same as a1
*/
System.out.println("\nThe size of a2 is " + a2.size());
for(int i=0;i<a2.size();i++)
{
System.out.println("The Value of a2 is " + a2.get(i));
}
ArrayList a3 = new ArrayList();
a3.addAll(0, a2);
/*
Not only that even if you add collection element a2 to addAll method of ArrayList a3
the size and value of a3 collection element remains the same as a1 or a2.
*/
System.out.println("\nThe size of a3 is " + a3.size());
for(int i=0;i<a3.size();i++)
{
System.out.println("The Value of a3 is " + a3.get(i));
}
}
}
Answer
--------
The size of a1 is 3
The Value of a1 is 1
The Value of a1 is 2
The Value of a1 is 3
The size of a2 is 3
The Value of a2 is 1
The Value of a2 is 2
The Value of a2 is 3
The size of a3 is 3
The Value of a3 is 1
The Value of a3 is 2
The Value of a3 is 3
addAll Method is defined as boolean addAll(int index, Collection c)
*/
import java.util.*;
public class addAll {
public static void main(String[] args) {
ArrayList a1 = new ArrayList();
for(int i=1;i<=3;i++)
{
a1.add(i);
}
System.out.println("The size of a1 is " + a1.size());
for(int i=0;i<a1.size();i++)
{
System.out.println("The Value of a1 is " + a1.get(i));
}
ArrayList a2 = new ArrayList();
a2.addAll(0, a1);
/*
Here if you see even after adding the collection element a1 to addAll method of ArrayList a2
the size and value of a2 collection element remains the same as a1
*/
System.out.println("\nThe size of a2 is " + a2.size());
for(int i=0;i<a2.size();i++)
{
System.out.println("The Value of a2 is " + a2.get(i));
}
ArrayList a3 = new ArrayList();
a3.addAll(0, a2);
/*
Not only that even if you add collection element a2 to addAll method of ArrayList a3
the size and value of a3 collection element remains the same as a1 or a2.
*/
System.out.println("\nThe size of a3 is " + a3.size());
for(int i=0;i<a3.size();i++)
{
System.out.println("The Value of a3 is " + a3.get(i));
}
}
}
Answer
--------
The size of a1 is 3
The Value of a1 is 1
The Value of a1 is 2
The Value of a1 is 3
The size of a2 is 3
The Value of a2 is 1
The Value of a2 is 2
The Value of a2 is 3
The size of a3 is 3
The Value of a3 is 1
The Value of a3 is 2
The Value of a3 is 3
No comments:
Post a Comment