Java Stack toArray(T[])方法与实例
Java中Stack类的 toArray(T[]) 方法是用来形成一个与Stack相同元素的数组。它返回一个包含该Stack中所有元素的数组 ,顺序正确; 返回的数组的运行时类型是指定数组的类型。如果Stack适合于指定的数组,它将被返回。否则,将分配一个新的数组,其运行时间类型为指定的数组,大小为该Stack的大小。
如果Stack适合在指定的数组中,并且还有剩余空间(即,数组的元素比Stack多),那么紧随Stack末端的数组中的元素将被设置为空。(只有当调用者知道Stack不包含任何空元素时,这对确定Stack的长度才是有用的。)
语法
Object[] arr1 = Stack.toArray(arr[])
参数: 该方法接受一个参数arr[],如果该数组足够大的话,它是存储Stack元素的数组;否则,将为此目的分配一个相同运行时类型的新数组。
返回值: 该方法返回一个包含类似于Stack元素的数组。
异常: 该方法可能抛出两种类型的异常。
- ArrayStoreException : 当提到的数组是不同的类型并且不能与Stack中提到的元素进行比较。
- NullPointerException : 如果数组是空的,就会抛出这个异常。
下面的程序说明了Stack.toArray(arr[])方法的工作。
程序1:当数组的大小与Stack相同时
// Java code to illustrate toArray(arr[])
import java.util.*;
public class StackDemo {
public static void main(String args[])
{
// Creating an empty Stack
Stack<String> stack = new Stack<String>();
// Use add() method to add elements into the Stack
stack.add("Welcome");
stack.add("To");
stack.add("Geeks");
stack.add("For");
stack.add("Geeks");
// Displaying the Stack
System.out.println("The Stack: " + stack);
// Creating the array and using toArray()
String[] arr = new String[5];
arr = stack.toArray(arr);
// Displaying arr
System.out.println("The arr[] is:");
for (int j = 0; j < arr.length; j++)
System.out.println(arr[j]);
}
}
输出:
The Stack: [Welcome, To, Geeks, For, Geeks]
The arr[] is:
Welcome
To
Geeks
For
Geeks
程序2:当阵列小于堆栈的大小时
// Java code to illustrate toArray(arr[])
import java.util.*;
public class StackDemo {
public static void main(String args[])
{
// Creating an empty Stack
Stack<String> stack = new Stack<String>();
// Use add() method to add elements into the Stack
stack.add("Welcome");
stack.add("To");
stack.add("Geeks");
stack.add("For");
stack.add("Geeks");
// Displaying the Stack
System.out.println("The Stack: " + stack);
// Creating the array and using toArray()
String[] arr = new String[1];
arr = stack.toArray(arr);
// Displaying arr
System.out.println("The arr[] is:");
for (int j = 0; j < arr.length; j++)
System.out.println(arr[j]);
}
}
输出:
The Stack: [Welcome, To, Geeks, For, Geeks]
The arr[] is:
Welcome
To
Geeks
For
Geeks
程序3:当阵列大于堆栈的大小时
// Java code to illustrate toArray(arr[])
import java.util.*;
public class StackDemo {
public static void main(String args[])
{
// Creating an empty Stack
Stack<String> stack = new Stack<String>();
// Use add() method to add elements into the Stack
stack.add("Welcome");
stack.add("To");
stack.add("Geeks");
stack.add("For");
stack.add("Geeks");
// Displaying the Stack
System.out.println("The Stack: " + stack);
// Creating the array and using toArray()
String[] arr = new String[10];
arr = stack.toArray(arr);
// Displaying arr
System.out.println("The arr[] is:");
for (int j = 0; j < arr.length; j++)
System.out.println(arr[j]);
}
}
输出:
The Stack: [Welcome, To, Geeks, For, Geeks]
The arr[] is:
Welcome
To
Geeks
For
Geeks
null
null
null
null
null
程序4:演示NullPointerException
// Java code to illustrate toArray(arr[])
import java.util.*;
public class StackDemo {
public static void main(String args[])
{
// Creating an empty Stack
Stack<String> stack = new Stack<String>();
// Use add() method to add elements into the Stack
stack.add("Welcome");
stack.add("To");
stack.add("Geeks");
stack.add("For");
stack.add("Geeks");
// Displaying the Stack
System.out.println("The Stack: " + stack);
try {
// Creating the array
String[] arr = null;
// using toArray()
// Since arr is null
// Hence exception will be thrown
arr = stack.toArray(arr);
// Displaying arr
System.out.println("The arr[] is:");
for (int j = 0; j < arr.length; j++)
System.out.println(arr[j]);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
输出:
The Stack: [Welcome, To, Geeks, For, Geeks]
Exception: java.lang.NullPointerException
极客教程