Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Java Collections. 3 Comments on ArrayList in Java Programming
0
(0)

This article provides a comprehensive overview of ArrayList in Java programming language along with example programs.

 

Introduction to ArrayList

 ArrayList class uses a dynamic array for storing a list of elements. It is inherited from AbstractList class and implements List interface. Following are some important points about ArrayList:

 

  • ArrayList can contain duplicate elements.
  • ArrayList size varies. When number of elements increase, size of ArrayList increases automatically, and when number of elements decrease, size of ArrayList also decreases.
  • ArrayList preserves the insertion order of elements.
  • ArrayList class is non-synchronized.
  • ArrayList allows random access of elements as it uses an array to store elements.
  • ArrayList is slow when insertion or deletion of elements is performed.

 

ArrayList Constructors

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


Following constructors are supported by the ArrayList class:

 

Constructor Description
ArrayList( ) Build an empty array list
ArrayList(Collection c) Builds an array list with the given collection c
ArrayList(int capacity) Builds an array list with specified initial capacity

 

ArrayList Methods

 

Following are some of the most frequently used methods of ArrayList:

 

Method Description
boolean add(Object o) Add the given element at the end of the list
void add(int index, Object element) Used to insert given element at the specified index in a list.
boolean addAll(Collection c) Add all the elements in the given collection c at the end of the list.
boolean addAll(int index, Collection c) Add the elements in the given collection at the end of the list.
void clear( ) Clear all the elements in the list and make it empty.
Object clone( ) Returns a shallow copy of the list
int indexOf(Object o) Returns the index of the given element on the list. Otherwise, returns -1 if the element is not present in the list.
int lastIndexOf(Object o) Returns the index of last occurrence of the given element in the list. Otherwise, returns -1 if the element is not present in the list.
Object[ ] toArray() Converts all the elements in the list to an array.
Object[ ] toArray(Object[ ] a) Converts all of the elements in this list in the correct order.

 

ArrayList Example

 

Following is an example program which demonstrates adding elements to a ArrayList and displaying them using a for  each loop:

 

import java.util.*;

public class Driver
{
	public static void main(String[] args)
	{
		ArrayList<String> friends = new ArrayList<String>();
		friends.add("Ramesh");
		friends.add("Mahesh");
		friends.add("Suresh");
		friends.add("Dinesh");
		friends.add("Ganesh");
		for(String item : friends)
		{
			System.out.println(item);
		}
	}
}

Output for the above program is as follows:

Ramesh
Mahesh
Suresh
Dinesh
Ganesh

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

3 Comments

You can follow any responses to this entry through the RSS 2.0 feed.

Java collections APIs provides lots of inbuilt classes and interfaces to handle collections of objects. It is very essential to learn and master java collections concepts. It is very important part of core java tutorial.

nice article for beginners.thank you.

Great post! The whole article about ArrayList were explained really nicely and as beginner it’s very easy to understand for me as well as others. keep up the great work!

Leave a Reply

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