Java Vector addAll()方法
java.util.Vector.addAll(Collection C)
该方法用于将作为参数传递给该函数的集合中的所有元素追加到一个向量的末尾,并牢记集合的迭代器的返回顺序。
语法:
boolean addAll(Collection C)
参数:该方法接受一个强制参数C,它是一个ArrayList的集合。它是一个集合,其元素需要被追加到向量的最后。
返回值:该方法如果至少执行了一个append的动作,则返回True,否则返回False。
下面的程序说明了Java.util.Vector.addAll()方法。
// Java code to illustrate boolean addAll()
import java.util.*;
import java.util.ArrayList;
public class GFG {
public static void main(String args[])
{
// Creating an empty Vector
Vector<String> vt = new Vector<String>();
// Use add() method to add elements in the Vector
vt.add("Geeks");
vt.add("for");
vt.add("Geeks");
vt.add("10");
vt.add("20");
// A collection is created
Collection<String> c = new ArrayList<String>();
c.add("A");
c.add("Computer");
c.add("Portal");
c.add("for");
c.add("Geeks");
// Displaying the Vector
System.out.println("The Vector is: " + vt);
// Appending the collection to the vector
vt.addAll(c);
// Clearing the vector using clear() and displaying
System.out.println("The new vector is: " + vt);
}
}
输出:
The Vector is: [Geeks, for, Geeks, 10, 20]
The new vector is: [Geeks, for, Geeks, 10, 20, A, Computer, Portal, for, Geeks]
java.util.Vector.addAll(int index, Collection C)
该方法用于在一个向量的特定索引或位置上追加作为参数传递给该函数的集合中的所有元素。
语法:
boolean addAll(int index, Collection C)
参数:该函数接受两个参数,如上面的语法所示,描述如下。
- index: 这个参数是整数数据类型,指定从容器中的元素开始插入矢量的位置。
- C : 它是一个ArrayList的集合。它是需要被追加的元素的集合。
返回值:该方法如果至少有一个append的动作被执行,则返回True,否则返回False。
下面的程序说明了Java.util.Vector.addAll()方法。
// Java code to illustrate boolean addAll()
import java.util.*;
import java.util.ArrayList;
public class GFG {
public static void main(String args[])
{
// Creating an empty Vector
Vector<String> vt = new Vector<String>();
// Use add() method to add elements in the Vector
vt.add("Geeks");
vt.add("for");
vt.add("Geeks");
vt.add("10");
vt.add("20");
// A collection is created
Collection<String> c = new ArrayList<String>();
c.add("A");
c.add("Computer");
c.add("Portal");
c.add("for");
c.add("Geeks");
// Displaying the Vector
System.out.println("The Vector is: " + vt);
// Appending the collection to the vector
vt.addAll(1, c);
// Clearing the vector using clear() and displaying
System.out.println("The new vector is: " + vt);
}
}
输出:
The Vector is: [Geeks, for, Geeks, 10, 20]
The new vector is: [Geeks, A, Computer, Portal, for, Geeks, for, Geeks, 10, 20]
极客教程