當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Java.util.ArrayList.addall()用法及代碼示例


以下是Java中ArrayList的addAll()方法:

  1. boolean addAll(Collection c):此方法將指定集合中的所有元素按指定集合的​​Iterator返回的順序附加到此列表的末尾。如果在操作進行過程中修改了指定的集合,則此操作的行為是不確定的(如果指定的集合是此列表,並且此列表是非空的,則表示此調用的行為是不確定的)。
    參數:
    c : This is the collection containing elements to be added to this list.
    Exception:
    NullPointerException : If the specified collection is null
    // Java program to illustrate 
    // boolean addAll(Collection c) 
    import java.io.*; 
    import java.util.ArrayList; 
      
    public class ArrayListDemo { 
        public static void main(String args[]) 
        { 
      
            // create an empty array list1 with initial  
            // capacity as 5 
            ArrayList<Integer> arrlist1 =  
                               new ArrayList<Integer>(5); 
      
            // use add() method to add elements in the list 
            arrlist1.add(12); 
            arrlist1.add(20); 
            arrlist1.add(45); 
      
            // prints all the elements available in list1 
            System.out.println("Printing list1:"); 
            for (Integer number : arrlist1)  
                System.out.println("Number = " + number);         
      
            // create an empty array list2 with an initial 
            // capacity 
            ArrayList<Integer> arrlist2 =  
                                 new ArrayList<Integer>(5); 
      
            // use add() method to add elements in list2 
            arrlist2.add(25); 
            arrlist2.add(30); 
            arrlist2.add(31); 
            arrlist2.add(35); 
      
            // let us print all the elements available in  
            // list2 
            System.out.println("Printing list2:"); 
            for (Integer number : arrlist2)  
                System.out.println("Number = " + number);         
      
            // inserting all elements, list2 will get printed 
            // after list1 
            arrlist1.addAll(arrlist2); 
      
            System.out.println("Printing all the elements"); 
            // let us print all the elements available in  
            // list1 
            for (Integer number : arrlist1)  
                System.out.println("Number = " + number);         
        } 
    }
    Output:Printing list1:
    Number = 12
    Number = 20
    Number = 45
    Printing list2:
    Number = 25
    Number = 30
    Number = 31
    Number = 35
    Printing all the elements
    Number = 12
    Number = 20
    Number = 45
    Number = 25
    Number = 30
    Number = 31
    Number = 35
    

    boolean addAll(int index,Collection c):此方法從指定位置開始將指定集合中的所有元素插入此列表。它將當前位於該位置的元素(如果有)和任何後續元素右移(增加其索引)。新元素將按照指定集合的​​迭代器返回的順序顯示在列表中。

    參數:
    index : The index at which to insert the first element from the specified collection.
    c : This is the collection containing elements to be added to this list.
    Exception:
    IndexOutOfBoundsException : If the index is out of range
    NullPointerException : If the specified collection is null
    // Java program to illustrate 
    // boolean addAll(int index, Collection c) 
    import java.io.*; 
    import java.util.ArrayList; 
      
    public class ArrayListDemo { 
        public static void main(String args[]) 
        { 
      
            // create an empty array list1 with initial 
            // capacity 5 
            ArrayList<Integer> arrlist =  
                            new ArrayList<Integer>(5); 
      
            // using add() method to add elements in the  
            // list 
            arrlist.add(12); 
            arrlist.add(20); 
            arrlist.add(45); 
      
            // prints all the elements available in list1 
            System.out.println("Printing list1:"); 
            for (Integer number : arrlist)  
                System.out.println("Number = " + number);         
      
            // create an empty array list2 with an initial 
            // capacity 
            ArrayList<Integer> arrlist2 =  
                                new ArrayList<Integer>(5); 
      
            // use add() method to add elements in list2 
            arrlist2.add(25); 
            arrlist2.add(30); 
            arrlist2.add(31); 
            arrlist2.add(35); 
      
            // prints all the elements available in list2 
            System.out.println("Printing list2:"); 
            for (Integer number : arrlist2)  
                System.out.println("Number = " + number);         
      
            // inserting all elements of list2 at third  
            // position 
            arrlist.addAll(2, arrlist2); 
      
            System.out.println("Printing all the elements"); 
      
            // prints all the elements available in list1 
            for (Integer number : arrlist)  
                System.out.println("Number = " + number);         
        } 
    }
    Output:Printing list1:
    Number = 12
    Number = 20
    Number = 45
    Printing list2:
    Number = 25
    Number = 30
    Number = 31
    Number = 35
    Printing all the elements
    Number = 12
    Number = 20
    Number = 25
    Number = 30
    Number = 31
    Number = 35
    Number = 45
    



相關用法


注:本文由純淨天空篩選整理自 Java.util.ArrayList.addall() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。