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