Java Tutorial series - ArrayList

ArrayList is one of the finest concepts of java. It is most widely used when one is dealing with dynamic arrays, ie resizable array which can be found it java.util package. It inherits the AbstractList class and implements the List interface.

ArrayList in Java
Few important points to consider -
  • ArrayList contains duplicate elements
  • ArrayList class contains elements in an insertion order.
  • ArrayList class is non-synchronized
  • List interface extends Collection and iterable interfaces.
By using ArrayList, below functions can come in handy -

  • add()
  • get()
  • set()
  • remove()
  • clear()
  • size()
How to iterate the elements of the collection in Java?
  • For Loop
  • For each loop
  • By iterator interface
Array List can be declared using below syntax -

ArrayList list = new ArrayList();
OR
ArrayList<Integer> number = new ArrayList<Integer>();
OR
List<String>  values = new ArrayList<String>();

number.add(10);
number.add(100);
number.add(50);

System.out.println(numbers.get(0));   // will give output as 10

 //For Loop Implementation using ArrayList
for(int i =0;i<numbers.size();i++)
{
System.out.println(number.get(i));
}

//Method 2 - using for each loop
for(Integer value : number) {
System.out.println(value);
}

  • ArrayList is slow as compared to LinkedList.


Keep Learning and keep sharing! 


Comments

Popular posts from this blog

Azure Tutorials Series - Azure Networking

Testing in CI/CD

Azure Tutorials Series - IaaS vs PaaS vs SaaS