在 Java 中将字符串集合转换为字符串数组
java 8object oriented programmingprogramming
首先,创建一个字符串集合 −
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("字符串集合:" + setStr); String[] arrStr = setStr.toArray(new String[0]); System.out.println("字符串数组 = "+ Arrays.toString(arrStr)); } }
输出
字符串集合:[Wordpres、Drupal、PrestaShop、osCommerce、Magento] 字符串数组 = [Wordpres、Drupal、PrestaShop、osCommerce、Magento]