Java Collections

ArrayList in java collections 

ArrayList is a class which implements List interface. It is widely used because of the functionality and flexibility it offers. Most of the developers choose Arraylist over Array as it’s a very good alternative of traditional java arrays.

- ArrayList stores the element’s value alone and internally maintains the indexes for each element.
ArrayList maintains the insertion order while HashMap doesn’t. Which means ArrayList returns the list items in the same order in which they got inserted into the list.
- ArrayList allows duplicate elements 
ArrayList can have any number of null elements.
ArrayList  get the element by specifying the index of it. 

Example Below : 

import java.util.ArrayList;

public class Details {
public static void main(String args[]) {
  ArrayList<String> arrStr = new ArrayList<String>();
  arrStr.add("Item1");
  arrStr.add("Item2");
  arrStr.add("Item6");
  arrStr.add("Item9");
  arrStr.add("Item8");
  arrStr.add("Item7");
  arrStr.add("Item3");
  arrStr.add("Item4");
   System.out.println("Item in array list are: "+arrStr);
  }

}
you will see the result like this : 
Item in array list are: [Item1, Item2, Item6, Item9, Item8, Item7, Item3, Item4]



Kategori

Kategori