Java ArrayList

0
32

In this post we will learn about Java ArrayList.

Java ArrayList class uses a dynamic array for storing the elements i.e. we don’t need to specify size as in case of traditional Array we used to do.

It extends AbstractList class and implements List interface.

ArrayList class can contain duplicate elements.

ArrayList class can contain elements of different data types.

ArrayList class maintains insertion order.
ArrayList class is non synchronized.

ArrayList allows random access because array works at the index basis.

In ArrayList class, manipulation with elements of ArrayList elements is slow because a lot of shifting needs to be done if any element is removed from the array list.

Let us see a simple ArrayList program:

 

  1. import java.util.ArrayList ;
  2.  
  3. public class Manager{
  4.  
  5. public static void main(String args[]){
  6.  
  7. ArrayList list= new ArrayList();
  8.  
  9. //can add any number of elements
  10.  
  11. list.add(90);
  12.  
  13. list.add(90.08);
  14.  
  15. list.add("abc");
  16.  
  17. list.add(true);
  18.  
  19. System.out.println(list);
  20.  
  21. System.out.println(list.get(2));// reading element at index 2
  22.  
  23. System.out.println(list);
  24.  
  25. }
  26.  
  27. }

OUTPUT:

[90,90.08,abc,true]

abc

[90,90.08,abc,true]

 

Facebook Comments
(Visited 39 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here