Java中的ConcurrentLinkedQueue toArray()方法
- toArray() : toArray() 方法是用于将ConcurrentLinkedQueue中的所有元素以正确的顺序返回一个与其相同的元素数组。基本上,它将所有元素从ConcurrentLinkedQueue复制到新数组中。该方法作为数组和ConcurrentLinkedQueue之间的桥梁。
语法:
public Object[] toArray()
返回值: 该方法返回一个包含与ConcurrentLinkedQueue相似的元素的数组。
下面的程序演示了java.util.concurrent.ConcurrentLinkedQueue.toArray()方法。
例1:
// Java程序演示了ConcurrentLinkedQueue的toArray()
// 方法
import java.util.concurrent.ConcurrentLinkedQueue;
public class GFG {
public static void main(String[] args)
{
// 创建ConcurrentLinkedQueue对象
ConcurrentLinkedQueue queue
= new ConcurrentLinkedQueue();
// 将元素添加到ConcurrentLinkedQueue
queue.add(2300);
queue.add(1322);
queue.add(8945);
queue.add(6512);
// 打印queue的详情
System.out.println("Queue Contains " + queue);
// 在queue上应用toArray()方法
Object[] array = queue.toArray();
// 打印数组的元素
System.out.println("The array contains:");
for (Object i : array) {
System.out.print(i + "\t");
}
}
}
输出:
Queue Contains [2300, 1322, 8945, 6512]
The array contains:
2300 1322 8945 6512
例2:
// Java程序演示了ConcurrentLinkedQueue的toArray()
// 方法
import java.util.concurrent.ConcurrentLinkedQueue;
public class GFG {
public static void main(String args[])
{
// 创建ConcurrentLinkedQueue对象
ConcurrentLinkedQueue
queue = new ConcurrentLinkedQueue();
// 将元素添加到队列
queue.add("Welcome");
queue.add("To");
queue.add("Jungle");
// 显示ConcurrentLinkedQueue
System.out.println("The ConcurrentLinkedQueue: "
+ queue);
// 创建数组并使用toArray()
Object[] arr = queue.toArray();
System.out.println("The array is:");
for (int j = 0; j < arr.length; j++)
System.out.println(arr[j]);
}
}
输出:
The ConcurrentLinkedQueue: [Welcome, To, Jungle]
The array is:
Welcome
To
Jungle
- toArray(T[] a) : toArray(T[] a) 方法用于以正确的顺序返回一个包含与此ConcurrentLinkedQueue相同元素的数组。该方法与toArray()只有一个条件不同。如果ConcurrentLinkedQueue大小小于或等于传递的数组,则返回的数组类型与参数数组相同。否则,将使用与指定数组相同类型的新数组进行分配,并且数组的大小等于此队列的大小。该方法作为数组和集合之间的桥梁。
语法:
public <T> T[] toArray(T[] a)
参数: 该方法接受一个数组a。
toArray(T[] a) 方法将队列中的所有元素复制到参数数组中(如果数组足够大)。否则,将为此分配一个相同类型的新数组运行时。
返回值: 此方法返回一个包含类似于 ConcurrentLinkedQueue 的元素的数组。
异常: 此方法抛出以下异常:
- ArrayStoreException :当传递的数组与 ConcurrentLinkedQueue 的元素的类型不同时。
- NullPointerException :如果传递的数组为空。
下面的程序说明了 java.util.concurrent.ConcurrentLinkedQueue.toArray(T[] a) 方法。
示例1:
// Java程序演示了 toArray()
// 方法的 ConcurrentLinkedQueue
import java.util.concurrent.ConcurrentLinkedQueue;
public class GFG {
public static void main(String[] args)
{
// 创建 ConcurrentLinkedQueue 的对象
ConcurrentLinkedQueue<String> queue
= new ConcurrentLinkedQueue<String>();
// 将元素添加到队列中
queue.add("Welcome");
queue.add("To");
queue.add("Jungle");
// 打印队列详细信息
System.out.println("Queue Contains " + queue);
// 要传递到 toArray() 的数组
// 数组的大小等于 ConcurrentLinkedQueue 的大小
String[] passArray = new String[queue.size()];
// 调用 toArray(T[] a) 方法
Object[] array = queue.toArray(passArray);
// 打印传递数组的元素
System.out.println("\n传递的数组 :");
for (String i : passArray) {
System.out.print(i + " ");
}
System.out.println();
// 打印返回数组的元素
System.out.println("\n返回的数组 :");
for (Object i : array) {
System.out.print(i + " ");
}
}
}
输出结果:
Queue Contains [Welcome, To, Jungle]
传递的数组 :
Welcome To Jungle
返回的数组 :
Welcome To Jungle
示例2: 演示 ArrayStoreException
// Java程序演示了 toArray()
// 方法的 ConcurrentLinkedQueue
import java.util.concurrent.ConcurrentLinkedQueue;
public class GFG {
public static void main(String[] args)
{
// 创建 ConcurrentLinkedQueue 的对象
ConcurrentLinkedQueue<Integer> queue
= new ConcurrentLinkedQueue<Integer>();
// 将元素添加到 ConcurrentLinkedQueue 中
queue.add(2323);
queue.add(2472);
queue.add(4235);
queue.add(1242);
// 要传递到 toArray() 的数组
// 数组的大小等于 ConcurrentLinkedQueue 的大小
String[] passArray = new String[queue.size()];
// 调用 toArray(T[] a) 方法
try {
Object[] array = queue.toArray(passArray);
}
catch (ArrayStoreException e) {
System.out.println("异常:" + e);
}
}
}
输出结果:
异常: java.lang.ArrayStoreException: java.lang.Integer
示例2: 演示 NullPointerException
// Java程序演示了 toArray()
// 方法的 ConcurrentLinkedQueue
import java.util.concurrent.ConcurrentLinkedQueue;
public class GFG {
public static void main(String[] args)
{
// 创建 ConcurrentLinkedQueue 的对象
ConcurrentLinkedQueue<String> queue
= new ConcurrentLinkedQueue<String>();
// 将元素添加到队列中
queue.add("Welcome");
queue.add("To");
queue.add("Jungle");
// 要传递到 toArray() 的数组
// 数组的大小等于 ConcurrentLinkedQueue 的大小
String[] passArray = null;
// 调用 toArray(T[] a) 方法
try {
Object[] array = queue.toArray(passArray);
}
catch (NullPointerException e) {
System.out.println("异常:" + e);
}
}
}
输出结果:
异常: java.lang.NullPointerException
极客教程