Created
April 29, 2020 18:54
-
-
Save singhrahuldps/6ee0777447d050ad81900de1f39842ca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Program to add and edit names in a list | |
| // ArrayList implements the List interface and are mutable | |
| ArrayList<String> names = new ArrayList<>(List.of("Rahul", "Virat", "Sourav")); | |
| // Elements can be added or removed from an ArrayList | |
| names.add("Rohit"); | |
| // This prints the elements of the list | |
| System.out.println(names); // [Rahul, Virat, Sourav, Rohit] | |
| // To iteratively delete elements from a list, | |
| // we must use the iterator class instead of looping over the array | |
| Iterator<String> itr = names.iterator(); // gets the iterator for names list | |
| String s; | |
| while(itr.hasNext()) { // while there is a next element | |
| s = itr.next(); | |
| if(s.endsWith("at")) { | |
| itr.remove(); | |
| } | |
| } | |
| System.out.println(names); // [Rahul, Sourav, Rohit] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment