java treeset 转array,Java TreeSet toArray(T[])用法及代碼示例
Java中TreeSet類的toArray(T [])方法方法用於生成與TreeSet相同元素的數組。它以正確的順序返回包含此TreeSet中所有元素的數組。返回數組的運行時類型是指定數組的運行時類型。如果TreeSet適合指定的數組,則在其中返回它。否則,將使用指定數組的運行時類型和此TreeSet的大小分配一個新數組。如果TreeSet適合指定的數組並有剩餘空間(即,數組比TreeSet包含更多的元素),則緊隨TreeSet結尾的數組中的元素設置為null。 (僅當調用者知道TreeSet不包含任何null元素時,此方法才可用於確定TreeSet的長度。)
用法:
public T[] toArray(T[] a)
參數:該方法接受一個參數arr [](如果它足夠大),它是TreeSet的元素要存儲到的數組;否則,將為此分配一個具有相同運行時類型的新數組。
返回值:該方法返回一個包含與TreeSet類似的元素的數組。
異常:該方法可能會引發兩種類型的異常:
ArrayStoreException:當提到的數組具有不同的類型並且無法與TreeSet中提到的元素進行比較時。
NullPointerException:如果數組為Null,則拋出此異常。
下麵的程序說明TreeSet.toArray(arr [])方法的用法。
程序1:當數組的大小為TreeSet的大小時
// Java code to illustrate toArray(arr[])
import java.util.*;
public class TreeSetDemo {
public static void main(String args[])
{
// Creating an empty TreeSet
TreeSet
set = new TreeSet();
// Use add() method to add
// elements into the TreeSet
set.add("Welcome");
set.add("To");
set.add("Geeks");
set.add("For");
set.add("Geeks");
// Displaying the TreeSet
System.out.println("The TreeSet:"
+ 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 TreeSet:[For, Geeks, To, Welcome]
The arr[] is:
For
Geeks
To
Welcome
null
程序2:當數組小於TreeSet的大小時
// Java code to illustrate toArray(arr[])
import java.util.*;
public class TreeSetDemo {
public static void main(String args[])
{
// Creating an empty TreeSet
TreeSet
set = new TreeSet();
// Use add() method to add
// elements into the TreeSet
set.add("Welcome");
set.add("To");
set.add("Geeks");
set.add("For");
set.add("Geeks");
// Displaying the TreeSet
System.out.println("The TreeSet:"
+ 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 TreeSet:[For, Geeks, To, Welcome]
The arr[] is:
For
Geeks
To
Welcome
程序3:當數組大於TreeSet的大小時
// Java code to illustrate toArray(arr[])
import java.util.*;
public class TreeSetDemo {
public static void main(String args[])
{
// Creating an empty TreeSet
TreeSet
set = new TreeSet();
// Use add() method to add
// elements into the TreeSet
set.add("Welcome");
set.add("To");
set.add("Geeks");
set.add("For");
set.add("Geeks");
// Displaying the TreeSet
System.out.println("The TreeSet:"
+ 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 TreeSet:[For, Geeks, To, Welcome]
The arr[] is:
For
Geeks
To
Welcome
null
null
null
null
null
null
程序4:演示NullPointerException
// Java code to illustrate toArray(arr[])
import java.util.*;
public class TreeSetDemo {
public static void main(String args[])
{
// Creating an empty TreeSet
TreeSet
set = new TreeSet();
// Use add() method to add
// elements into the TreeSet
set.add("Welcome");
set.add("To");
set.add("Geeks");
set.add("For");
set.add("Geeks");
// Displaying the TreeSet
System.out.println("The TreeSet:"
+ 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 TreeSet:[For, Geeks, To, Welcome]
Exception:java.lang.NullPointerException
總結
以上是生活随笔為你收集整理的java treeset 转array,Java TreeSet toArray(T[])用法及代碼示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中的静态初始化是什么意思,Jav
- 下一篇: acm java输入输出_在竞赛ACM