Created
December 24, 2021 08:16
-
-
Save gauravkukade/b90f2ca5a9f605cc96ba0abe5ffc8270 to your computer and use it in GitHub Desktop.
A java program to remove all the elements of ArrayList using the clear() method. Read more at https://coderolls.com/arraylist-clear-method-in-java/
This file contains 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
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* A java program to remove all the elements | |
* of ArrayList using the clear() method. | |
* | |
* @author Gaurav Kukade at coderolls.com | |
* | |
*/ | |
public class ArrayListClearMethodExample { | |
public static void main(String[] args) { | |
// create an arraylist cities | |
List<String> cities = new ArrayList<String>(); | |
// add string objects in 'cities' | |
cities.add("New York City"); | |
cities.add("Los Angeles"); | |
cities.add("Mountain View"); | |
cities.add("Austin"); | |
cities.add("Atlanta"); | |
// printing the cities | |
System.out.println("Before invoking the clear() method"); | |
System.out.println("Cities: "+ cities); //print the arraylist with elements | |
System.out.println("cities size: "+ cities.size()); | |
//invoke the clear() method on arraylist | |
cities.clear(); | |
System.out.println("\nAfter invoking the clear() method"); | |
System.out.println("Cities: "+ cities); // empty arraylist | |
System.out.println("cities size: "+ cities.size()); | |
} | |
} |
This file contains 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
Before invoking the clear() method | |
Cities: [New York City, Los Angeles, Mountain View, Austin, Atlanta] | |
cities size: 5 | |
After invoking the clear() method | |
Cities: [] | |
cities size: 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment