Categories
collections java

ArrayList in java with example

 

In this tutorial we will learn ArrayList in java with example 

In Java we can achieve the dynamic array using arraylist.
ArrayList class available on util package, so we don’t need to import any extra packages, In below example 

  • created new arraylist
  • added value to arraylist
  • printing the array list value by using for loop 

Example

import java.util.ArrayList;
import java.util.List;

public class ArrayListExample {
	  public static void main(String[] args) {
      List arrayList = new ArrayList();
      arrayList.add("Java");
      arrayList.add("Java 8");
      arrayList.add("Java 11");
      for (int i = 0; i < arrayList.size(); i++) {
    System.out.println("Value at the index of " + i + " "+arrayList.get(i));
     }
   }
}

Output

Value at the index of 0 Java
Value at the index of 1 Java 8
Value at the index of 2 Java 11
Github

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/ArrayListExample.java

Related Articles

Sort an Arraylist in java with example

iterate stream with index in Java8

 

Leave a Reply

Your email address will not be published. Required fields are marked *