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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class App { static ArrayList<String> listNames = new ArrayList<>(); public static void main(String[] args) { listNames.add("Chaand"); listNames.add("John"); listNames.add("Steve"); listNames.add("Pooja"); listNames.add("Salim"); listNames.add("Rahul"); listNames.add("Mia"); listNames.add("Angel"); System.out.println(listNames); } } |
Output
1 |
[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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
public class App { static ArrayList<String> listNames = new ArrayList<>(); public static void main(String[] args) { listNames.add("Chaand"); listNames.add("John"); listNames.add("Steve"); listNames.add("Pooja"); listNames.add("Salim"); listNames.add("Rahul"); listNames.add("Mia"); listNames.add("Angel"); App app = new App(); app.displayList(listNames); /* app.removeNameByPosition(0); System.out.println("************"); app.displayList(listNames); System.out.println("************"); System.out.println(listNames.get(0)); System.out.println(listNames.get(1)); */ System.out.println("************"); app.removeNameByString("Pooja"); app.displayList(listNames); System.out.println("************"); System.out.println(listNames.get(3)); } void displayList(ArrayList<String> names){ for(String name: names){ System.out.println(name); } } void removeNameByPosition(int position){ listNames.remove(position); } void removeNameByString(String name){ listNames.remove(name); } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
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”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
public class App { static ArrayList<String> listNames = new ArrayList<>(); public static void main(String[] args) { listNames.add("Chaand"); listNames.add("John"); listNames.add("Steve"); listNames.add("Pooja"); listNames.add("Salim"); listNames.add("Rahul"); listNames.add("Mia"); listNames.add("Angel"); App app = new App(); app.displayList(listNames); System.out.println("************"); int position = app.search("Pooja"); if(position != -1){ app.modifyName(position, "Emma"); app.displayList(listNames); }else{ System.out.println("Invalid entry"); } } void displayList(ArrayList<String> names){ for(String name: names){ System.out.println(name); } } void modifyName(int position, String newName){ listNames.set(position, newName); } int search(String name){ return listNames.indexOf(name); } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
Chaand John Steve Pooja Salim Rahul Mia Angel ************ Chaand John Steve Emma Salim Rahul Mia Angel |
Contributed by Poonam Tomar