Home » Core Java » util » ArrayList

ArrayList

Arraylist in Java Example – How to use arraylist

In this example we will show how to use ArrayList in Java. The class java.util.ArrayList provides resizable-array, which means that items can be added and removed from the list. It implements the List interface. A major question related to arraylists is about when to use arraylists instead of arrays and vice versa. An ArrayList is a dynamic data structure so ...

Read More »

Sort ArrayList example using Collections sort

In this example we shall show you how to sort an ArrayList using sort operation of the Collections API. To sort an ArrayList using Collections sort one should perform the following steps: Create a new ArrayList. Populate the arrayList with elements, using add(E e) API method of ArrayList. Invoke sort(List list) API method of Collections to sort the arrayList elements ...

Read More »

Search elements in ArrayList example

With this example we are going to demonstrate how to search elements in a ArrayList. In short, to search elements in a ArrayList you should: Create a new ArrayList. Populate the arrayList with elements, using add(E e) API method of ArrayList. Check if an element exists in the arrayList, withcontains(Object element) API method of ArrayList. The method returns true if ...

Read More »

Replace ArrayList elements using index example

This is an example of how to replace elements in an ArrayList using index. Replacing elements in an ArrayList using index implies that you should: Create a new ArrayList. Populate the arrayList with elements, using add(E e) API method of ArrayList. Use set(int index, Object obj) method of ArrayList, using a specified element and a specified index. The method replaces ...

Read More »

Remove ArrayList elements using index example

In this example we shall show you how to remove an ArrayList‘s elements using the elements index. To remove an element from an ArrayList using its index one should perform the following steps: Create a new ArrayList. Populate the arrayList with elements, using add(E e) API method of ArrayList. Remove an element from the arrayList, using a specified index, using ...

Read More »

Remove all elements from ArrayList example

With this example we are going to demonstrate how to remove all elements from an ArrayList, that means clearing the arrayList. In short, to clear an ArrayList you should: Create a new ArrayList. Populate the arrayList with elements, using add(E e) API method of ArrayList. Invoke clear() API method of ArrayList. The method removes all of the elements from this ...

Read More »

ArrayList ListIterator example

This is an example of how to obtain an ArrayList ListIterator. The ListIterator is an iterator for lists that allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator’s current position in the list. Obtaining an ArrayList ListIterator implies that you should: Create a new ArrayList. Populate the arrayList with elements, ...

Read More »

ArrayList Iterator example

In this example we shall show you how to obtain an ArrayList Iterator, that is an iterator over the elements of the ArrayList. To obtain an ArrayList Iterator one should perform the following steps: Create a new ArrayList. Populate the arrayList with elements, using add(E e) API method of ArrayList. Use iterator() API method to obtain the Iterator of the ...

Read More »

Insert all elements of Collection to specific ArrayList index

With this example we are going to demonstrate how to insert all elements of a Collection to a specific ArrayList index. We are using the Vector as a Collection implementation. In short, to insert all elements of a Vector to a specific ArrayList index you should: Create a new ArrayList. Populate the arrayList with elements, using add(E e) API method ...

Read More »

Get sub list of ArrayList example

This is an example of how to get a subList of an ArrayList, that is a list containing elements from a startIndex to an endIndex of the ArrayList. Getting a subList of an ArrayList implies that you should: Create a new ArrayList. Populate the arrayList with elements, using add(E e) API method of ArrayList. Use the subList(int startIndex, int endIndex) ...

Read More »

Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns