11.01. ArrayList

ArrayList

  • Eclipse: Oxygen
  • Java: 1.8

Here all the basic operations associated with the Arraylists are shown such as 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

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

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

Contributed by Poonam Tomar

 

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments