首先,创建一个字符串集-
Set<String> setStr = new HashSet<>(Arrays.asList("osCommerce", "PrestaShop", "Magento","Wordpres", "Drupal"));
现在,使用toArray()方法转换为字符串数组-
String[] arrStr = setStr.toArray(new String[0]);
以下是在Java中将字符串集转换为字符串数组的程序-
import java.util.Arrays; import java.util.Set; import java.util.HashSet; public class Demo { public static void main(String[] args) { Set<String> setStr = new HashSet<>( Arrays.asList("osCommerce", "PrestaShop", "Magento","Wordpres", "Drupal")); System.out.println("Set of String: " + setStr); String[] arrStr = setStr.toArray(new String[0]); System.out.println("Array of String = "+ Arrays.toString(arrStr)); } }
输出结果
Set of String: [Wordpres, Drupal, PrestaShop, osCommerce, Magento] Array of String = [Wordpres, Drupal, PrestaShop, osCommerce, Magento]