热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Java中TreeSettoArray(T[])方法,示例

Java中TreeSettoArray(T[])方法,示例

Java 中 TreeSet toArray(T[])方法,示例

原文:https://www . geesforgeks . org/treeset-to arrayt-method-in-Java-with-example/

Java 中 TreeSet 类toArray(T[]) 方法方法用于形成与 TreeSet 相同元素的数组。它以正确的顺序返回一个包含该树集中所有元素的数组返回数组的运行时类型是指定数组的运行时类型。如果 TreeSet 适合指定的数组,它将在其中返回。否则,将使用指定数组的运行时类型和此 TreeSet 的大小分配一个新数组。
如果 TreeSet 适合有空余空间的指定数组(即数组中的元素比 TreeSet 多),则紧接着 TreeSet 末尾的数组中的元素被设置为空。(只有当调用者知道树集合不包含任何空元素时,这在确定树集合的长度时才有用。)

语法:

public <T> T[] toArray(T[] a)

参数:该方法接受一个参数 arr[] ,如果树集合的元素足够大,则该参数是要存储到其中的数组;否则,将为此目的分配一个相同运行时类型的新数组。

返回值:该方法返回一个包含类似于 TreeSet 元素的数组。

异常:该方法可能会引发两种类型的异常:


  • arraystorexception:当提到的数组属于不同类型,无法与 TreeSet 中提到的元素进行比较时。

  • NullPointerException :如果数组为 Null,那么抛出这个异常。

下面的程序说明了 TreeSet.toArray(arr[])方法的工作原理。

程序 1: 当数组有树集大小时

// Java code to illustrate toArray(arr[])
import java.util.*;
public class TreeSetDemo {
    public static void main(String args[])
    {
        // Creating an empty TreeSet
        TreeSet<String>
            set = new TreeSet<String>();
        // 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]);
    }
}

Output:

The TreeSet: [For, Geeks, To, Welcome]
The arr[] is:
For
Geeks
To
Welcome
null

程序 2: 当数组小于树集的大小时

// Java code to illustrate toArray(arr[])
import java.util.*;
public class TreeSetDemo {
    public static void main(String args[])
    {
        // Creating an empty TreeSet
        TreeSet<String>
            set = new TreeSet<String>();
        // 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]);
    }
}

Output:

The TreeSet: [For, Geeks, To, Welcome]
The arr[] is:
For
Geeks
To
Welcome

程序 3: 当数组大于树集的大小时

// Java code to illustrate toArray(arr[])
import java.util.*;
public class TreeSetDemo {
    public static void main(String args[])
    {
        // Creating an empty TreeSet
        TreeSet<String>
            set = new TreeSet<String>();
        // 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]);
    }
}

Output:

The TreeSet: [For, Geeks, To, Welcome]
The arr[] is:
For
Geeks
To
Welcome
null
null
null
null
null
null

程序 4: 演示空指针异常

// Java code to illustrate toArray(arr[])
import java.util.*;
public class TreeSetDemo {
    public static void main(String args[])
    {
        // Creating an empty TreeSet
        TreeSet<String>
            set = new TreeSet<String>();
        // 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);
        }
    }
}

Output:

The TreeSet: [For, Geeks, To, Welcome]
Exception: java.lang.NullPointerException


推荐阅读
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • 本文介绍了Java高并发程序设计中线程安全的概念与synchronized关键字的使用。通过一个计数器的例子,演示了多线程同时对变量进行累加操作时可能出现的问题。最终值会小于预期的原因是因为两个线程同时对变量进行写入时,其中一个线程的结果会覆盖另一个线程的结果。为了解决这个问题,可以使用synchronized关键字来保证线程安全。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 从零学Java(10)之方法详解,喷打野你真的没我6!
    本文介绍了从零学Java系列中的第10篇文章,详解了Java中的方法。同时讨论了打野过程中喷打野的影响,以及金色打野刀对经济的增加和线上队友经济的影响。指出喷打野会导致线上经济的消减和影响队伍的团结。 ... [详细]
author-avatar
李小白无悔
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有