Java中AbstractCollection toArray()方法的示例
1. toArray()
Java中的AbstractCollection的 toArray() 方法用于形成与AbstractCollection相同元素的数组。基本上,它会将所有元素从AbstractCollection复制到一个新的数组中。
语法:
Object[] arr = AbstractCollection.toArray()
参数: 该方法没有参数。
返回值: 该方法返回一个包含与AbstractCollection类似的元素的数组。
下面的程序说明了AbstractCollection.toArray()方法:
程序1:
// Java代码示例:illustrate toArray()
import java.util.*;
public class AbstractCollectionDemo {
public static void main(String args[])
{
// 创建一个空的AbstractCollection
AbstractCollection<String>
abs_col = new PriorityQueue<String>();
// 使用add()方法将元素添加到AbstractCollection中
abs_col.add("Welcome");
abs_col.add("To");
abs_col.add("Geeks");
abs_col.add("For");
abs_col.add("Geeks");
// 显示AbstractCollection
System.out.println("AbstractCollection: "
+ abs_col);
// 创建数组并使用toArray()
Object[] arr = abs_col.toArray();
System.out.println("数组是:");
for (int j = 0; j < arr.length; j++)
System.out.println(arr[j]);
}
}
AbstractCollection: [For, Geeks, To, Welcome, Geeks]
数组是:
For
Geeks
To
Welcome
Geeks
程序2:
// Java代码示例:illustrate toArray()
import java.util.*;
public class AbstractCollectionDemo {
public static void main(String args[])
{
// 创建一个空的AbstractCollection
AbstractCollection<Integer>
abs_col = new PriorityQueue<Integer>();
// 使用add()方法将元素添加到AbstractCollection中
abs_col.add(10);
abs_col.add(15);
abs_col.add(30);
abs_col.add(20);
abs_col.add(5);
abs_col.add(25);
// 显示AbstractCollection
System.out.println("AbstractCollection: "
+ abs_col);
// 创建数组并使用toArray()
Object[] arr = abs_col.toArray();
System.out.println("数组是:");
for (int j = 0; j < arr.length; j++)
System.out.println(arr[j]);
}
}
AbstractCollection: [5, 10, 25, 20, 15, 30]
数组是:
5
10
25
20
15
30
2. toArray(arr[])
Java 中 AbstractCollection 类的 toArray(arr[]) 方法用于形成一个与 AbstractCollection 具有相同元素的数组。它以正确的顺序返回包含此 AbstractCollection 中所有元素的数组,返回的数组的运行时类型是指定数组的类型。如果 AbstractCollection 能够适应指定的数组,则返回指定的数组。否则,将分配具有指定数组的运行时类型和 AbstractCollection 大小的新数组。
如果 AbstractCollection 能够适应带有多余空间的指定数组(即数组元素多于 AbstractCollection),则该数组极为抽象集合后面的元素被设置为 null。 (仅在调用程序知道 AbstractCollection 不包含任何 null 元素时,这对于确定 AbstractCollection 的长度很有用。)
语法:
Object[] arr1 = AbstractCollection.toArray(arr[])
参数: 该方法接受一个名为 arr[] 的参数,如果足够大,则将 AbstractCollection 的元素存储在其中;否则,将为此目的分配具有相同运行时类型的新数组。
返回值: 该方法返回一个包含类似于 AbstractCollection 的元素的数组。
异常: 该方法可能会抛出两种类型的异常:
- ArrayStoreException:当提到的数组是不同类型的并且无法与 AbstractCollection 中提到的元素进行比较时。
- NullPointerException:如果数组为空,则抛出此异常。
下面的程序说明了 AbstractCollection.toArray(arr[]) 方法的工作方式。
程序 1:当数组的长度等于 AbstractCollection 的长度
// Java code to illustrate toArray(arr[])
import java.util.*;
public class AbstractCollectionDemo {
public static void main(String args[])
{
// Creating an empty AbstractCollection
AbstractCollection<String>
abs_col = new PriorityQueue<String>();
// Use add() method to add
// elements into the AbstractCollection
abs_col.add("Welcome");
abs_col.add("To");
abs_col.add("Geeks");
abs_col.add("For");
abs_col.add("Geeks");
// Displaying the AbstractCollection
System.out.println("The AbstractCollection: "
+ abs_col);
// Creating the array and using toArray()
String[] arr = new String[5];
arr = abs_col.toArray(arr);
// Displaying arr
System.out.println("The arr[] is:");
for (int j = 0; j < arr.length; j++)
System.out.println(arr[j]);
}
}
The AbstractCollection: [For, Geeks, To, Welcome, Geeks]
The arr[] is:
For
Geeks
To
Welcome
Geeks
程序 2:当数组的长度小于 AbstractCollection 的长度
// Java代码演示toArray(arr[])方法
import java.util.*;
public class AbstractCollectionDemo {
public static void main(String args[])
{
// 创建一个空的AbstractCollection
AbstractCollection<String>
abs_col = new PriorityQueue<String>();
// 使用add()方法向AbstractCollection添加元素
abs_col.add("欢迎");
abs_col.add("来到");
abs_col.add("Geeks");
abs_col.add("For");
abs_col.add("Geeks");
// 显示AbstractCollection
System.out.println("AbstractCollection: "
+ abs_col);
// 创建数组并使用toArray()
String[] arr = new String[1];
arr = abs_col.toArray(arr);
// 显示arr数组
System.out.println("arr[]是:");
for (int j = 0; j < arr.length; j++)
System.out.println(arr[j]);
}
}
AbstractCollection: [For, Geeks, To, 欢迎, Geeks]
arr[]是:
For
Geeks
To
欢迎
Geeks
程序3: 当数组大小大于AbstractCollection时
// Java代码演示toArray(arr[])方法
import java.util.*;
public class AbstractCollectionDemo {
public static void main(String args[])
{
// 创建一个空的AbstractCollection
AbstractCollection<String>
abs_col = new PriorityQueue<String>();
// 使用add()方法向AbstractCollection添加元素
abs_col.add("欢迎");
abs_col.add("来到");
abs_col.add("Geeks");
abs_col.add("For");
abs_col.add("Geeks");
// 显示AbstractCollection
System.out.println("AbstractCollection: "
+ abs_col);
// 创建数组并使用toArray()
String[] arr = new String[10];
arr = abs_col.toArray(arr);
// 显示arr数组
System.out.println("arr[]是:");
for (int j = 0; j < arr.length; j++)
System.out.println(arr[j]);
}
}
AbstractCollection: [For, Geeks, To, 欢迎, Geeks]
arr[]是:
For
Geeks
To
欢迎
Geeks
null
null
null
null
null
程序4: 演示NullPointerException
// Java代码演示toArray(arr[])方法
import java.util.*;
public class AbstractCollectionDemo {
public static void main(String args[])
{
// 创建一个空的AbstractCollection
AbstractCollection<String>
abs_col = new PriorityQueue<String>();
// 使用add()方法向AbstractCollection添加元素
abs_col.add("欢迎");
abs_col.add("来到");
abs_col.add("Geeks");
abs_col.add("For");
abs_col.add("Geeks");
// 显示AbstractCollection
System.out.println("AbstractCollection: "
+ abs_col);
try {
// 创建数组
String[] arr = null;
// 使用toArray()
// 因为arr是null
// 所以会抛出异常
arr = abs_col.toArray(arr);
// 显示arr数组
System.out.println("arr[]是:");
for (int j = 0; j < arr.length; j++)
System.out.println(arr[j]);
}
catch (Exception e) {
System.out.println("异常: " + e);
}
}
}
AbstractCollection: [For, Geeks, To, Welcome, Geeks]
异常: java.lang.NullPointerException