ArrayList

  • Eclipse: Oxygen
  • Java: 1.8

Here all the basic operation associated with the Arraylists are shown such as the append the name to the ArrayList, remove by position, remove by the string ( case sensitive) and also get the position of the element given.

The following example demonstrates:

  • Creation of an ArrayList
  • Addition of new elements to an ArrayList using the add() method

Output

[Chaand, John, Steve, Pooja, Salim, Rahul, Mia, Angel]

Consider the following amazing experiment which shows:

  • Removal of the element by position
  • Removal of the element by object name

In this code names are stored in a list,  removeNameByString() method is used to remove “Pooja” string from the listNames.

Output

Chaand

John

Steve

Pooja

Salim

Rahul

Mia

Angel

************

Chaand

John

Steve

Salim

Rahul

Mia

Angel

************

Salim

ArrayList in Java is used to store a collection of dynamic-sized elements. Unlike arrays that have a fixed size, an ArrayList automatically increases its size when new elements are added.

The further implementation of search by position using indexOf() function and also the modification entity with the help of function is shown here.

In the following program, we can search an element from the list, if the element is available in the list, it returns the index of that element else it returns -1. If it found the element “Pooja” in the list then modify the name with “Emma”.

Output

Chaand

John

Steve

Pooja

Salim

Rahul

Mia

Angel

************

Chaand

John

Steve

Emma

Salim

Rahul

Mia

Angel

Contributed by Poonam Tomar

 


Full stack Java Developer

   Buy me a coffee!

 

PayTM QR (INDIA)

Download file

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu