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.
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.
- 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>();
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
Post a Comment