📜  从Java中的 ArrayList 中删除所有元素

📅  最后修改于: 2022-05-13 01:55:03.424000             🧑  作者: Mango

从Java中的 ArrayList 中删除所有元素


先决条件: Java中的 ArrayList

给定一个 ArrayList,任务是在Java中删除 ArrayList 的所有元素。

例子:

Input: ArrayList = [1, 2, 3, 4] 
Output: ArrayList = [] 

Input: ArrayList = [12, 23, 34, 45, 57, 67, 89] 
Output: ArrayList = [] 
  • 使用 clear() 方法:

    句法:

    collection_name.clear();

    clear() 方法的代码:

    public void clear() {
        for (int i = 0; i < size; i++)
            list[i] = null;
    
        size = 0;
    }
    

    下面是上述方法的实现:

    // Java Program for remove all elements ArrayList
    // Using clear() method
      
    // import  ArrayList package
    import java.util.ArrayList;
      
    public class GFG {
      
        // main method
        public static void main(String[] args)
        {
            // create empty ArrayList
            ArrayList list = new ArrayList<>();
      
            // Adding elements of list
            list.add("Geeks");
            list.add("for");
            list.add("Geeks");
            list.add("Gaurav");
      
            // printing initial value ArrayList
            System.out.println("ArrayList: " + list);
      
            // print size of ArrayList
            System.out.println("Size of ArrayList = "
                               + list.size());
      
            // remove all elements using clear() method
            list.clear();
      
            // printing ArrayList
            System.out.println("\nAfter clear\n\n"
                             + "ArrayList: " + list);
      
            // print size of ArrayList after clear list
            System.out.println("Size of ArrayList = "
                               + list.size());
        }
    }
    
    输出:
    ArrayList: [Geeks, for, Geeks, Gaurav]
    Size of ArrayList = 4
    
    After clear
    
    ArrayList: []
    Size of ArrayList = 0
    

    时间复杂度:O(N)

  • 使用 removeAll() 方法

    句法:

    collection_name.removeAll(collection_name);

    removeAll() 方法的代码:

    public boolean removeAll(Collection list) {
        boolean isModi = false;
        Iterator ite= iterator();
        while (ite.hasNext()) {
            if (list.contains(ite.next())) {
                ite.remove();
                isModi = true;
            }
        }
        return isModi;
    }
    

    下面是上述方法的实现:

    // Java Program for remove all elements ArrayList
    // Using removeAll() method
      
    // import  ArrayList package
    import java.util.ArrayList;
      
    public class GFG {
      
        // main method
        public static void main(String[] args)
        {
            // create empty ArrayList
            ArrayList list = new ArrayList<>();
      
            // Adding elements of list
            list.add("Geeks");
            list.add("for");
            list.add("Geeks");
            list.add("Gaurav");
      
            // printing initial value ArrayList
            System.out.println("ArrayList: " + list);
      
            // print size of ArrayList
            System.out.println("Size of ArrayList = "
                               + list.size());
      
            // remove all elements using clear() method
            list.removeAll(list);
      
            // printing ArrayList
            System.out.println("\nAfter clear\n\n"
                             + "ArrayList: " + list);
      
            // print size of ArrayList after clear list
            System.out.println("Size of ArrayList = "
                               + list.size());
        }
    }
    
    输出:
    ArrayList: [Geeks, for, Geeks, Gaurav]
    Size of ArrayList = 4
    
    After clear
    
    ArrayList: []
    Size of ArrayList = 0
    

    时间复杂度:O(N^2)