CodeGym /Java Blog /Toto sisi /Java ArrayList 的 addAll() 方法
John Squirrels
等級 41
San Francisco

Java ArrayList 的 addAll() 方法

在 Toto sisi 群組發布
如果需要將一個ArrayList()集合的所有元素添加到另一個集合中,可以使用Java ArrayList類的addAll()方法。在本文中,我將解釋此方法的工作原理並提供一些代碼示例。

ArrayList addAll() 方法簽名

java.util.ArrayList.addAll方法有兩種變體。他們來了。

boolean addAll(Collection<? extends E> c)
如果列表因調用而更改,則此方法返回 true,但最重要的是,它將指定集合的​​所有元素添加到此列表的末尾,按照它們由指定集合的​​迭代器返回的順序。元素 c 是將添加到ArrayList中的列表。如果指定的集合為空, 該方法也會拋出NullPointerException 。

boolean addAll(int index, Collection<? extends E> c)
此方法將指定集合c 中的所有元素插入此列表,從指定位置索引開始。

ArrayList addAll() 語法示例

這是在 Java 程序中調用 ArrayList addAll()方法的樣子:

myList.addAll(myNewList)) 
其中myList是原始ArrayListmyNewList是一個列表,如果存在, 則將其所有值添加到myList 。

myList.addAll(2, myNewList)) 
具有兩個參數的ArrayList addAll()方法 的變體。myNewList中的所有元素將添加到myList中,第一個元素myNewList[0]將變為myList[2]myNewList[1]現在是myList[3],依此類推。原始列表中的元素,從編號 2,被轉移到新組合列表的尾部。

ArrayList addAll() 代碼示例

現在是一些代碼示例的時候了。讓我們創建一個朋友列表,並將其稱為“朋友”,並在其中添加他們的名字。比如說,我們開始上大學並立即結交了一些新朋友。現在是為他們創建新朋友列表的好時機。時間過去了,你確定你的新朋友是真實的一次,所以你決定將他們移到主要朋友列表中。您可以使用addAll()方法來完成。在程序中,我們會將初始好友列表、新好友列表、更新後的好友列表以及 addAll() 方法返回的布爾值打印到控制台。如果原始列表由於方法的操作而更改,則返回 true,如果未發生,則返回 false。

import java.util.ArrayList;
import java.util.List;

public class AddAllDemo {
       public static void main(String[] args) {

           List<String> friends = new ArrayList<>();
           friends.add("Johnny");
           friends.add("Ivy");
           friends.add("Rick");
           System.out.println(friends);
           List<String> newFriends = new ArrayList<>();
           newFriends.add("Andrew");
           newFriends.add("Alex");
           newFriends.add("Neil");
           System.out.println(newFriends);
           //let's print out the value addAll() method returns
           //here it's true because list friends was changed
           System.out.println(friends.addAll(newFriends));
           System.out.println("My new list with new friends added: ");
           System.out.println(friends);
       }
   }
輸出是:
[Johnny, Ivy, Rick] [Andrew, Alex, Neil] 是的 我添加了新朋友的新列表:[Johnny, Andrew, Alex, Neil, Ivy, Rick]
如果我們做同樣的操作,但嘗試向朋友添加一個空列表,該方法會工作,但它會返回 false,而不是 true,因為原始列表沒有改變。

import java.util.ArrayList;
       import java.util.List;

public class AddAllDemo3 {
   public static void main(String[] args) {
       List<String> friends = new ArrayList<>();
       friends.add("Johnny");
       friends.add("Ivy");
       friends.add("Rick");
       System.out.println(friends);
       List<String> newFriends = new ArrayList<>();
       System.out.println(newFriends);
       System.out.println(friends.addAll(newFriends));
       System.out.println("My new list with new friends added: ");
       System.out.println(friends);
   }
}
輸出是:
[Johnny, Ivy, Rick] [] false 我添加了新朋友的新列表:[Johnny, Ivy, Rick]
現在讓我們嘗試使用兩個參數的addAll()方法。

boolean addAll(int index, Collection<? extends E> c)
讓我們再次將新朋友添加到列表中,不是在尾部,而是在中間。假設緊接在 Johnny 之後,在我們的例子中,它是編號為零的元素。

import java.util.ArrayList;
import java.util.List;

public class AddAllDemo2 {
   public static void main(String[] args) {

       List<String> friends = new ArrayList<>();
       friends.add("Johnny");
       friends.add("Ivy");
       friends.add("Rick");
       System.out.println(friends);
       List<String> newFriends = new ArrayList<>();
       newFriends.add("Andrew");
       newFriends.add("Alex");
       newFriends.add("Neil");
       System.out.println(newFriends);
       System.out.println(friends.addAll(1, newFriends));
       System.out.println("My new list with new friends added: ");
       System.out.println(friends);
   }
}
該程序工作的預期結果:Andrew 現在在合併列表中排名第一,Alex - 2,Neil - 3,而之前排名第一的 Ivy 已移至第 4 位。
[Johnny, Ivy, Rick] [Andrew, Alex, Neil] 是的 我添加了新朋友的新列表:[Johnny, Andrew, Alex, Neil, Ivy, Rick]
留言
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION