Disadvantages of arrays:




Advantages / Benefits of arraylist in java:

  • We have some disadvantages of arrays like arrays are fixed in length. and we need to mention size of the array while creation itself.
  • So we have some advantages of arraylist when compared to arrays in java.
  • Here  the major advantages of arraylist over arrays.

1.ArrayList is variable length
  • One of the major benefit of arraylist is it is dynamic in size. we can increase as well as decrease size of the arraylist dynamically.
  • Resizable.
Program #1: Java example program to explain the advantages of ArrayList: resizable


  1. package collections;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class ArrayListExample {
  6.     /**
  7.     * Advantages of arrayList over arrays in java
  8.     * @author www.instanceofjava.com
  9.     */
  10.  public static void main(String[] args) {
  11.         
  12.         ArrayList<Integer> list = new ArrayList<Integer>();
  13.          
  14.         list.add(1);
  15.  
  16.         list.add(2);
  17.  
  18.         list.add(3);
  19.  
  20.         System.out.println(list.size());      
  21.         list.add(4);
  22.         list.add(5);
  23.  
  24.         System.out.println(list.size());     
  25.         list.remove(1);
  26.  
  27.         System.out.println(list.size());    
  28.     }
  29. }

Output:

  1. 3
  2. 5
  3. 4
  • In the above program by using add and remove methods of ArrayList we can change the size of the ArrayList

2.Default initial capacity is 10.
  • One of the major benefit of arraylist is by default it will assign default size as 10.
  • Whenever we create object of ArrayList constructor of ArrayList assign default capacity as 10.

3.Insert and remove elements also at particular position of  ArrayList
  • Another advantage of ArrayList is it can add and remove elements at particular position.

Program #2: Java example program to explain the advantages of ArrayList:Add and remove elements at particular position.

  1. package collections;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class ArrayListExample {
  6.     /**
  7.     * Advantages of arrayList over arrays in java
  8.     * @author www.instanceofjava.com
  9.     */
  10.     public static void main(String[] args) {
  11.         
  12.         ArrayList<Integer> list = new ArrayList<Integer>();
  13.          
  14.         list.add(1);
  15.  
  16.         list.add(2);
  17.  
  18.         list.add(3);
  19.  
  20.         System.out.println(list);
  21.  
  22.         list.add(0,4);
  23.         
  24.         System.out.println(list);
  25.         
  26.         list.add(2,5);
  27.  
  28.       
  29.         System.out.println(list);
  30.         list.remove(3);
  31.         
  32.         System.out.println(list);
  33.         System.out.println(list.size());    
  34.     }
  35. }

Output:

  1. [1, 2, 3]
  2. [4, 1, 2, 3]
  3. [4, 1, 5, 2, 3]
  4. [4, 1, 5, 3]
  5. 4


4.Add any type of data into ArrayList.
  • We can add different type of objects in to the ArrayList.
  • In this scenario avoid mentioning generics while declaring ArrayList.

Program #3: Java example program to explain the advantages of ArrayList: Add any type of Object.

arrayList advantages in java

5.Traverse in both directions.
  • We can traverse both direction using List Iterator.

Program #4: Java example program to explain the advantages of ArrayList: Traverse in both directions.
  1. package collections;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.ListIterator;
  5.  
  6. public class ArrayListExample {
  7.     /**
  8.     * Advantages of arrayList over arrays in java
  9.     * @author www.instanceofjava.com
  10.     */
  11.     public static void main(String[] args) {
  12.         
  13.         ArrayList<String> list = new ArrayList<String>();
  14.          
  15.         list.add("ArrayList Advantages");
  16.  
  17.         list.add("ArrayList benefits");
  18.         
  19.         list.add("ArrayList in java");
  20.  
  21.         ListIterator iterator = list.listIterator();
  22.         
  23.         System.out.println("**ArrayList Elements in forward direction**");
  24.          
  25.         while (iterator.hasNext())
  26.         {
  27.             System.out.println(iterator.next());
  28.         }
  29.          
  30.         System.out.println("**ArrayList Elements in backward direction**");
  31.          
  32.         while (iterator.hasPrevious())
  33.         {
  34.             System.out.println(iterator.previous());
  35.         } 
  36.     }
  37. }
 Output:

  1. **ArrayList Elements in forward direction**
  2. ArrayList Advantages
  3. ArrayList benefits
  4. ArrayList in java
  5. **ArrayList Elements in backward direction**
  6. ArrayList in java
  7. ArrayList benefits
  8. ArrayList Advantages

6.ArrayList allows Multiple null values
  • We can add multiple null elements to ArrayList

Program #5: Java example program to explain the benefits of ArrayList: Add null elements
  1. package collections;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class ArrayListExample {
  6.     /**
  7.     * Advantages of arrayList over arrays in java
  8.     * @author www.instanceofjava.com
  9.     */
  10.     public static void main(String[] args) {
  11.         
  12.         ArrayList<String> arraylist = new ArrayList<String>();
  13.          
  14.         arraylist.add(null);
  15.  
  16.         arraylist.add(null);
  17.         
  18.         arraylist.add(null);
  19.  
  20.         System.out.println(arraylist);
  21.     }
  22. }

Output:

  1. [null, null, null]

7.ArrayList allows to add duplicate elements
  • We can add duplicate elements into arrayList
Program #6: Java example program to explain the advantages of ArrayList: Add any type of Object.
  1. package collections;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class ArrayListExample {
  6.     /**
  7.     * Advantages of arrayList over arrays in java
  8.     * @author www.instanceofjava.com
  9.     */
  10.     public static void main(String[] args) {
  11.         
  12.         ArrayList<String> arraylist = new ArrayList<String>();
  13.          
  14.        arraylist.add("ArrayList");
  15.  
  16.         arraylist.add("ArrayList");
  17.         
  18.         arraylist.add("ArrayList");
  19.  
  20.         System.out.println(arraylist);
  21.     }
  22. }

Output:

  1. [ArrayList, ArrayList, ArrayList]

8.ArrayList has many methods to manipulate stored objects.
  • ArrayList has many methods to manipulate stored objects.
  • addAll(), isEmpty(). lastIndexOf() etc.

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu