ARRAYLIST METHODS(ADD)

ArrayList methods(add)


How to add or insert the elements in to ArrayList(Array List methods)?

In ArrayList we can add elements in 5 different ways, they are By Using
  • add(Object value)
  • add( int index , Object objectValue)
  • addAll(Collection c)
  • addAll(int index, Collection c)
  • ArrayList(Collection c)

Adding elements to ArrayList By add(Object value) method:

    This method is used for adding an object to the Collection object. When we use this method to add the object to Collection objects and List object, this method always returns true. Since, Collection object and List object allows duplicates. When we apply the same method with Set and SortedSet methods and when we are try to add duplicates, this method returns false.

    Note

      Whatever the data we want to add through collection framework object that primitive data must be converted into the corresponding wrapper class object and all the objects of wrapper.

    Syntax:

      public boolean add (java.lang.Object):

Example 1:

 
						
              package com.sdj.pack1;
              import java.util.ArrayList;
              public class ArList
              {
                 public static void main(String[] args)
                 {
                    ArrayList list=new ArrayList();
                    list.add(10);
                    list.add(20);
                    list.add(30);
                    list.add(40);
                    list.add(10);
                    
                    /* Here we can add duplicate value i.e 10. so  arraylist */
                    /* can allow duplicate values also. */
                    list.add(20);
                    System.out.println(list);
                 }
              } 
                

Output:

[10, 20, 30, 40, 10, 20]

Example 2:

 
						
              package com.sdj.pack1;
              public class ArList
              {
                 public static void main(String[] args)
                 {
                    ArrayList list=new ArrayList();
                    list.add("10");
                    list.add("abc");
                    list.add(30.30f);
                    list.add(null);
                    list.add(10);
                    list.add(20);
                    System.out.println(list);
                 }
              } 
                

Output:

[10, abc, 30.3, null, 10, 20]
    Note
    • ArrayList can take different types of data. Even we can add null values also.
    • According to jdk1.5 we can able to pass primitive values why because here Auto boxing is available.

Example 3:

 
						
               package com.sdj.pack1;
               public class ArList
               {
                  public static void main(String[] args)
                  {
                     ArrayList list1=new ArrayList();
                     list1.add(10);
                     list1.add(20);
                     List list2=new ArrayList();
                     list2.add(10);
                     list2.add(20);
                     System.out.println(list1.equals(list2));
                     System.out.println(list1.hashCode()==list2.hashCode());
                     System.out.println(list1);
                  }
               }                

Output:

true true [10, 20]
    Note
      In ArrayList the toString (), hashCode () and equals () were overridden according to based on the content, not based on the memory location. Since from the above example list1 and list2 objects are different list objects but we are getting output as “TRUE” from equals() and hashcode() methods. Due to both are containing same data .

2ndway Of Adding Elements in to List is Insert at specified location:

Add(index, objectValue)

If we want to insert/add values randomly at specified location into the ArrayList we can go for add(int index, Object objvalue) method.

    Syntax:

    Add(int index, Object objectvalue);

    Note
      If we insert the new value at specified location. Then the old values which are present in current location will shift to the next location and next value is shift to the next location ….next…..next…… this process will continue till last element of the list.

Example 4:

 
						
               package com.sdj.pack1;
               public class ArList
               {
                  public static void main(String[] args)
                  {
                     ArrayList list1=new ArrayList();
                     list1.add(10);
                     list1.add(20);
                     list1.add(30);
                     list1.add(60);
                     list1.add(70);
                     System.out.println(list1);
                     list1.add(3,40);
                     System.out.println(list1);
                     list1.add(4,50);
                     System.out.println(list1);
                     /* Here we are adding   values  i,e . 40,50 through the */
                     /* index. Then  40 will be placed on the  3rd index position */
                     /* and then 50 will be placed on the 4th index position, and */
                     /* the values that are there in corresponding location */
                     /* will shift to next location. */
                  }
               }                     	

Output:

Java Setup Wizard
Adding Elements in to List example

Example 5:

 
						
               package com.sdj.pack1;
               public class ArList
               {
                  public static void main(String[] args)
                  {
                     ArrayList list1=new ArrayList();
                     list1.add(10);
                     list1.add(20);
                     list1.add(30);
                     
                     /* list1.add(4,40); */
                     /* Here exception will be raised why because we can’t */
                     /* add/insert new element after end index. If we try to */
                     /* add any element out of the index range (i.e. < 0 or >maxIndex ) */
                     /* then add() will raise exception i.ejava.lang.IndexOutOfBoundsException */
                     /* list1.add(-1,50); // here also same problem like above */
                     /* System.out.println(list1); */
                  }
               }                        

Output:

     
     
     

Explanation:

  • If we want to add any element by using specified index. That index should be between 0 and size i.e. starting index and ending index.

  • Index should be >=0 and < size.

Example 6:

 
						
               package com.sdj.pack1;
               public class ArList
               {
                  public static void main(String[] args)
                  {
                     List list1=new ArrayList();
                     list1.add(10);
                     list1.add(20);
                     System.out.println(list1);
                     list1.add(0,30);
                     list1.add(list1.size(),40);
                     System.out.println(list1);
                     list1.add(10/5,50);
                     System.out.println(list1);
                  }
               }                

Output:

[10, 20] [30, 10, 20, 40] [30, 10, 50, 20, 40]

Example 7:

 
						
               package com.sdj.pack1;
               public class ArList
               {
                  public static void main(String[] args)
                  {
                     List list1=new ArrayList();
                     list1.add(10);
                     list1.add(20);
                     System.out.println(list1);
                     List list2=new ArrayList();
                     list2.add(30);
                     list2.add(40);
                     list2.add(list1);
                     /* if we add one collection object to another collection */
                     /* object it will add like entire object as single element */
                     /* to the current collection object. */
                     
                     System.out.println(list2);
                  }
               }                        
                

Output:

[10, 20] [30, 40, [10, 20]]// here the bold text is adding list1 object to list2 object.

3rd way of adding elements to the list:

addAll(Collection c):

    It will add all elements from one collection Object to another collection Object.

Example 8:

 
						
               package com.sdj.pack1;
               public class ArList
               {
                  public static void main(String[] args)
                  {
                     List list1=new ArrayList();
                     list1.add(10);
                     list1.add(20);
                     System.out.println(list1);
                     List list2=new ArrayList();
                     list2.add(30);
                     list2.add(40);
                     list2.addAll(list1);
                     System.out.println(list2);
                  }
               }                

Output:

[10, 20] [30, 40, 10, 20]

Explanation:

  • Here addAll() method will take another Collection object as input and Add all objects as individual elements into new Collection object .

  • From the above example we are passing list1 object into list2 object using addAll() method. so it is adding all objects which are available in list1 object to list2 object as indusial elements.

  • Note
      If we add one collection object to add(); method, then result will be added as entire list object is one single element.
    Java Setup Wizard
    explanation.

4th way of adding:

addAll(int index,Collection c):

    This method is used for adding one Collection object elements to another Collection object at the specified position as indusial elements.

Example 9:

 
						
               package com.sdj.pack1;
               import java.util.ArrayList;
               import java.util.List;
               public class Collctn
               {
                  public static void main(String[] args)
                  {
                     List l1=new ArrayList();
                     l1.add(10);
                     l1.add(20);
                     l1.add(60);
                     l1.add(70);
                     System.out.println(l1);
                     List l2=new ArrayList();
                     l2.add(30);
                     l2.add(40);
                     l2.add(90);
                     l2.addAll(2,l1);
                     System.out.println(l2);
                  }
               }                

Output:

[10, 20, 60, 70] --- list1 result IndExample 0 1 2 [30, 40, 90] --- list2 result before calling addAll(); IndExample 0 1 2 3 4 5 6 [30, 40, 10, 20, 60, 70, 90] --- list2 result after calling addAll();

5th way of adding elements to the list through Constructor :

ArrayList(Collection c):

    adding one collection object elements to another collection object through constructor, it same as addAll() method.

Example 10:

 
						
               package com.sdj.pack1;
               public class Collctn
               {
                  public static void main(String[] args)
                  {
                     ArrayList l1=new ArrayList();
                     /* Here we will create  EmptyArrayList with  zero size */
                     l1.add(10);
                     l1.add(20);
                     l1.add(30);
                     System.out.println(l1);
                     ArrayList l2=new ArrayList(l1);
                     /* Here we will create ArrayList  Object with “l1” list */
                     /* elements are as a initial values .*/
                     System.out.println(l2);
                  }
               }                

Output:

[10, 20, 30] ----- list1 result [10, 20, 30] ----- list2 result, it is creating with elements of list1 elements as initial elements to list2 .

Example 11:

 
						
               package com.sdj.pack1;
               public class ArrList
               {
                  public static void main(String[] args)
                  {
                     ArrayList l1=new ArrayList();
                     l1.add("abc");
                     l1.add(45);
                     System.out.println(l1);
                     ArrayListl2=new ArrayList();
                     l2.add("xyz");
                     System.out.println(l2);
                     l2.add(l1);
                     /* Here we are adding  “l1” collection object to l2 object .*/
                     System.out.println(l2);
                     l2.add(l2);
                     /* Here we are adding  “l2” collection object to l2 only .*/
                     System.out.println(l2);
                     l2.addAll(l2);
                     /* Here also  we are  adding  “l2” collection object to l2 */ 
                     /* Object only by using addAll() method */
                     System.out.println(l2);
                  }
               }                

Output:

[abc, 45] --- result of list1 [xyz] --- result of list2 initial [xyz, [abc, 45] ] --- result of list2 after calling l2.add(l1); [xyz, [abc, 45], (this Collection)] --- result of list2 after calling l2.add(l2); [xyz, [abc, 45], (this Collection), xyz, [abc, 45], (this Collection)] --result of list2 after calling l2.addAll(l2) /* Here this Collection refers to the current Object i.e,”l2”. */
    Note 1
      If we try to add any collection object to same collection object by using add(Collection obj) method, then it will add that object value as “(this Collection)”,But not add like other collection object. i.e not like ---[xyz, [abc, 45]]
    Note 2
      addAll() will works same for both cases either if we add the same Collection objector other Collection object, The result will be same.

    For example:

      List list1 = new ArrayList();
      list1.add(20);
      list1.add(30);
      List list2 = new ArrayList();
      list2.add(40);
      list2.add(50);
      System.out.println(l2); output ------- [40,50]
      list2.addAll(l2); output--------[40,50,40,50] Both case same
      list2.addAll(l1); output--------[40,50,40,50,20,30]