How to empty an array in Java

Java 8Object Oriented ProgrammingProgramming

Use List.clear() method to empty an array.

Example

import java.util.ArrayList;
import java.util.List;
public class Tester {
   public static void main(String[] args) {
      List<String> list = new ArrayList<>();
      list.add("A");
      list.add("B");
      list.add("C");
      list.add("D");
      list.add("E");
      list.add("F");
      list.add("G");
      list.add("H");
      System.out.println("Original List " + list);
      list.clear();
      System.out.println("Cleared List " + list);
   }
}

Output

Original List [A, B, C, D, E, F, G, H]
Cleared List []
raja
Published on 06-Feb-2018 15:54:59
Advertisements