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[])用法及代碼示例相关推荐

  1. java dayofweek_Java DayOfWeek getDisplayName()用法及代碼示例

    java.time.DayOfWeek的getDisplayName()方法是Java中的一個內置函數,它根據指定的Locale類參數和TextStyle返回day-of-week的文本表示形式. T ...

  2. java中decrement,Java LongAdder decrement()用法及代碼示例

    Java中的LongAdder類創建一個初始和為零的新加法器. Java.LongAdder.decrement()是Java中的內置方法,可將值減少1. 用法: public void decrem ...

  3. 检验signature的java代码,Java Signature initSign()用法及代碼示例

    initSign(PrivateKey privateKey) java.security.Provider類的initSign()方法用於初始化此對象以進行簽名.如果使用不同的參數再次調用此方法,則 ...

  4. java file.canexecute_Java File canExecute()用法及代碼示例

    canExecute()函數是Java中File類的一部分.此函數確定程序是否可以執行由抽象路徑名表示的指定文件.如果文件路徑存在並且允許應用程序執行文件,則此方法將返回true.否則它將返回fals ...

  5. java hypot_Java StrictMath hypot()用法及代碼示例

    根據基本幾何形狀,斜邊僅是right-angled三角形的最長邊.它是與三角形直角相反的一側.為了找到right-angled三角形的斜邊的長度,應用勾股定理.根據該定理,給定長度為p和b的三角形的兩 ...

  6. java roll_Java Calendar roll()用法及代碼示例

    Calendar類中的roll(int calndr_field,boolean up_down)方法用於通過上下移動傳遞的字段單個時間單位來對傳遞的日曆字段進行操作.這涉及在不更改較大字段的情況下對 ...

  7. java中getfield_Java Class getField()用法及代碼示例

    java.lang.Class類的getField()方法用於獲取此類的指定字段,該字段是公共字段及其成員.該方法以Field對象的形式返回此類的指定字段. 用法: public Field getF ...

  8. java intfunction_Java IntFunction類代碼示例

    本文整理匯總了Java中java.util.function.IntFunction類的典型用法代碼示例.如果您正苦於以下問題:Java IntFunction類的具體用法?Java IntFunct ...

  9. java json parser_Java JSONParser.parse方法代碼示例

    本文整理匯總了Java中org.json.simple.parser.JSONParser.parse方法的典型用法代碼示例.如果您正苦於以下問題:Java JSONParser.parse方法的具體 ...

最新文章

  1. Java transient
  2. java安全例外_java – 本地Applet安全例外
  3. mysql8.0 本地监听端口_mysql8.0启动后不能正常监听端口的问题处理
  4. jcenter那些事儿
  5. 深入分析linux内核源码
  6. 华为h12m03装系统_华为H22H-03服务器怎么从U盘装系统?
  7. 【内网穿透路由器】外网环境下使用Frp内网穿透路由器,实现后台的远程访问
  8. 工作到现在的一些感想
  9. Nuvoton M0518 之 看门狗的使用Demo
  10. 无法查看别的计算机,雨林木风win7网上邻居看不到别的电脑的解决教程
  11. sql server日期格式转换方法大全
  12. 附解决方案,小程序用户昵称突然变成了“微信用户”,而且头像也显示不了?
  13. 《红楼梦》中四大家族到底犯了什么罪,导致了「白茫茫大地真干净」的结局?
  14. 浅谈人脸识别在公共安全领域的应用
  15. 调用本地主干的预训练的.pth文件
  16. html 输入框联动显示,js下拉选择框与输入框联动实现添加选中值到输入框的方法...
  17. 华为HCIP RS题库221 561-570题
  18. Quill富文本的使用以及自定义图片和视频处理事件
  19. 天籁obd接口针脚定义_2013新天籁加装OBD胎压监测+无损改装彻底解决亏电求加精...
  20. 2020淘宝618全自动源码 v1.0.0

热门文章

  1. jQuery EasyUI 折叠面板accordion的使用实例
  2. 80070583类不存在_结合JVM源码谈Java类加载器
  3. Intellij IDEA 2019 自动生成 serialVersionUID
  4. 外部中断0——51程序
  5. python分组求和_Python学习笔记之pandas索引列、过滤、分组、求和功能示例
  6. GIt思维导图命令+案例分析
  7. 运用HashMap和ArrayList打造一个简单的带文件的控制台学生管理系统(附上类及类方法的思维导图+控制台运行界面截图+源代码)
  8. 大众mpv_一汽-大众全新MPV车型国内伪装路试曝光,没有侧滑门设计
  9. C51对标准C语言的扩展 / 数据类型
  10. 计算机网络知识培训计划,计算机网络管理员教学计划和大纲