ArrayList in java

ArrayList in java is most common Collections data structure along with HashMap which we use very often.

Why to choose ArrayList vs Array:

  1. Array is fixed length data structure If array is full , you can not add element to it, where as ArrayList in java can dynamically grow and shrink as per our need.
  2. You can use generics with ArrayList but not with Array
  3. ArrayList have predefined methods which can be used to perform operations.

Some important points about ArrayList in java

  1. ArrayList is implementation of list interface.
  2. ArrayList is not synchonized(so not thread safe)
  3. ArrayList is implemented using array as internal data structure.It can be dynamically resized .
  4. ArrayList increases half of its size when its size is increased.

ArrayList

Example of ArrayList in java

When you run above program, you will get below output:

Iterate over list

We have used foreach loop to iterate over list in above example: There are many ways to iterate over list. Some of them are:

  1. Using for loop
  2. Using Iterator

Iterator example:

When you run above program, you will get below output:

Methods of ArrayList in java

I am explaining below method in context of [String list](https://java2blog.com/list-string-java/ “String list”), but you can put any object in the list.

add( Object o): This method adds an object o to end of the arraylist.

This statement would add a string Amit in the ArrayList at last position.

add(int index, Object o): This adds the object o to the arraylist at the specified index.

It will add the string Ankit to the 5th index (6rd position as the array list starts with index 0) of ArrayList.

Object get(int index): It returns the object of list which is present at the specified index.

It is used to get element from ArrayList and this is most used method. Here we are getting element from ArrayList and assigning it to String object

remove(Object o): Removes the object o from the ArrayList.

This code will remove the string “Arpit” from the ArrayList.

remove(int index): Removes element which is present at specified index

It would remove the element of index 3 (4th element of the list as list always starts with 0).

set(int index, Object o): It is mainly used for updating an element. It replaces the element present at the specified index with the object o.

It would replace element which is present at index 2(3rd element in arrayList)with the value Tom.

int indexOf(Object o): It is used to find index of object o. If object is not present, this method returns -1

This would give the index (position) of the string Ankit in employeeNameList.

int size(): It gives the size of the ArrayList

boolean contains(Object o): 
It checks whether the given object o is present in the ArrayList or not. If not present, it returns false

It would return true if the string “Amit” is present in the list else it will return false

clear():

It will remove all the object of ArrayList, so above statement will remove all String object from employeeNameList

addAll(Collection c)
If you want to merge two ArrayList, you can use this method.
Example:
When you run above program, you will get below output:
removeAll(Collection c)
If you want to remove list of elements which is present in other list, you can use this method.
Example:
When you run above program, you will get below output:
retainAll(Collection)
If you want to retain only those elements which are present in Collection, you can use this method.
Example:
When you run above program, you will get below output:

That’s all about ArrayList in java.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 06 December

    Update Value of Key in HashMap in Java

    Table of ContentsUsing the put() Method of HashMap Collection in JavaUsing the compute() Method of HashMap Collection in JavaUsing the merge() Method of the HashMap Collection in JavaUsing the computeIfPresent() Method of The HashMap Collection in JavaUsing the replace() Method of The HashMap Collection in JavaUsing the TObjectIntHashMap Class of Gnu.Trove Package in JavaUsing the […]

  • 29 November

    Create Array of Linked Lists in Java

    Table of ContentsIntroductionLinked List in JavaApplication of Array of Linked ListsCreate Array of Linked Lists in JavaUsing Object[] array of Linked Lists in JavaUsing the Linked List array in JavaUsing the ArrayList of Linked Lists in JavaUsing the Apache Commons Collections Package Introduction In this article, we will look at how to Create an Array […]

  • 20 September

    Return ArrayList in Java

    Table of ContentsReturn ArrayList in Java From a Static MethodReturn ArrayList in Java From a Non-static MethodConclusion This article discusses cases of returning an ArrayList in Java from a method. An ArrayList in Java is a collection of elements of the same data type under a single variable name. In different cases, you can return […]

  • 17 September

    Create List with One Element in Java

    Table of ContentsUsing Collections.singletonList()Using Array.asList() method [ Immuatable list]Using new ArrayList with Array.asList() method [ Mutable list] In this post, we will see how to create List with One Element in java.. Using Collections.singletonList() This is best way to create List with single element if you need an immutable List. [crayon-63dacf714b08e511998789/] Output [crayon-63dacf714b091717619826/] If you […]

  • 17 May

    How to Add Multiple Values for Single Key In HashMap in Java

    Table of ContentsHashMapCan HashMap Store Multiple Values AutomaticallyWays to Add Multiple Values for Single Key In HashMap in JavaUsing the Standard LibraryUsing Apache Commons LibraryUsing Google Guava LibraryUsing TreeSet as ValuesUsing a Wrapper ClassUsing Java TuplesUsing compute() Function in JDK 8Conclusion This article discusses the HashMap in Java and how to add multiple values for […]

  • java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
    22 February

    [Fixed] java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

    Table of ContentsReason for java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayListFixes for java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayListUse ArrayList’s constructorAssign Arrays.asList() to List reference rather than ArrayList In this post, we will see how to fix java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList. ClassCastException is runtime exception which indicate that code has tried to […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.