Skip to content

Instantly share code, notes, and snippets.

@singhrahuldps
Created April 29, 2020 18:54
Show Gist options
  • Select an option

  • Save singhrahuldps/6ee0777447d050ad81900de1f39842ca to your computer and use it in GitHub Desktop.

Select an option

Save singhrahuldps/6ee0777447d050ad81900de1f39842ca to your computer and use it in GitHub Desktop.
// 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