当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java HashSet toArray(T[])用法及代码示例


Java中HashSet类的toArray(T [])方法方法用于生成与HashSet元素相同的元素的数组。它以正确的顺序返回包含此HashSet中所有元素的数组。返回数组的运行时类型是指定数组的运行时类型。如果HashSet适合指定的数组,则在其中返回它。否则,将使用指定数组的运行时类型和此HashSet的大小分配一个新数组。如果HashSet适合指定的数组并有剩余空间(即,数组比HashSet包含更多的元素),则紧随HashSet结束的数组中的元素设置为null。 (仅当调用者知道HashSet不包含任何null元素时,这才对确定HashSet的长度很有用。)

用法:

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

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


返回值:该方法返回一个包含与HashSet相似的元素的数组。

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

  • ArrayStoreException:当提到的数组具有不同的类型并且无法与HashSet中提到的元素进行比较时。
  • NullPointerException :如果数组为Null,则抛出此异常。

以下示例程序旨在说明HashSet.toArray(arr [])方法的用法。

程序1:当数组的大小为HashSet时

// Java code to illustrate toArray(arr[]) 
  
import java.util.*; 
  
public class HashSetDemo { 
    public static void main(String args[]) 
    { 
        // Creating an empty HashSet 
        HashSet<String> 
            hashSet = new HashSet<String>(); 
  
        // Use add() method to add 
        // elements into the HashSet 
        hashSet.add("Welcome"); 
        hashSet.add("To"); 
        hashSet.add("Geeks"); 
        hashSet.add("For"); 
        hashSet.add("Geeks"); 
  
        // Displaying the HashSet 
        System.out.println("The HashSet:"
                           + hashSet); 
  
        // Creating the array and using toArray() 
        String[] arr = new String[5]; 
        arr = hashSet.toArray(arr); 
  
        // Displaying arr 
        System.out.println("The arr[] is:"); 
        for (int j = 0; j < arr.length; j++) 
            System.out.println(arr[j]); 
    } 
}
输出:
The HashSet:[Geeks, For, Welcome, To]
The arr[] is:
Geeks
For
Welcome
To
null

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

// Java code to illustrate toArray(arr[]) 
  
import java.util.*; 
  
public class HashSetDemo { 
    public static void main(String args[]) 
    { 
        // Creating an empty HashSet 
        HashSet<String> 
            hashSet = new HashSet<String>(); 
  
        // Use add() method to add 
        // elements into the HashSet 
        hashSet.add("Welcome"); 
        hashSet.add("To"); 
        hashSet.add("Geeks"); 
        hashSet.add("For"); 
        hashSet.add("Geeks"); 
  
        // Displaying the HashSet 
        System.out.println("The HashSet:"
                           + hashSet); 
  
        // Creating the array and using toArray() 
        String[] arr = new String[1]; 
        arr = hashSet.toArray(arr); 
  
        // Displaying arr 
        System.out.println("The arr[] is:"); 
        for (int j = 0; j < arr.length; j++) 
            System.out.println(arr[j]); 
    } 
}
输出:
The HashSet:[Geeks, For, Welcome, To]
The arr[] is:
Geeks
For
Welcome
To

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

// Java code to illustrate toArray(arr[]) 
  
import java.util.*; 
  
public class HashSetDemo { 
    public static void main(String args[]) 
    { 
        // Creating an empty HashSet 
        HashSet<String> 
            hashSet = new HashSet<String>(); 
  
        // Use add() method to add 
        // elements into the HashSet 
        hashSet.add("Welcome"); 
        hashSet.add("To"); 
        hashSet.add("Geeks"); 
        hashSet.add("For"); 
        hashSet.add("Geeks"); 
  
        // Displaying the HashSet 
        System.out.println("The HashSet:"
                           + hashSet); 
  
        // Creating the array and using toArray() 
        String[] arr = new String[10]; 
        arr = hashSet.toArray(arr); 
  
        // Displaying arr 
        System.out.println("The arr[] is:"); 
        for (int j = 0; j < arr.length; j++) 
            System.out.println(arr[j]); 
    } 
}
输出:
The HashSet:[Geeks, For, Welcome, To]
The arr[] is:
Geeks
For
Welcome
To
null
null
null
null
null
null

程序4:演示NullPointerException

// Java code to illustrate toArray(arr[]) 
  
import java.util.*; 
  
public class HashSetDemo { 
    public static void main(String args[]) 
    { 
        // Creating an empty HashSet 
        HashSet<String> 
            hashSet = new HashSet<String>(); 
  
        // Use add() method to add 
        // elements into the HashSet 
        hashSet.add("Welcome"); 
        hashSet.add("To"); 
        hashSet.add("Geeks"); 
        hashSet.add("For"); 
        hashSet.add("Geeks"); 
  
        // Displaying the HashSet 
        System.out.println("The HashSet:"
                           + hashSet); 
  
        try { 
            // Creating the array 
            String[] arr = null; 
            // using toArray() 
            // Since arr is null 
            // Hence exception will be thrown 
            arr = hashSet.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 HashSet:[Geeks, For, Welcome, To]
Exception:java.lang.NullPointerException


相关用法


注:本文由纯净天空筛选整理自Code_r大神的英文原创作品 HashSet toArray(T[]) method in Java with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。