如何将Set直接转成数组

Set直接转成数组

例子

public class Test{
    
    public void test1(){
        Set set = new HashSet<>();
        set.add("1");
        set.add("2");
        set.add("3");
        set.add("4");
        set.add("5");
        set.add("6");
    }
}

错误方式:

String[] arr = (String[]) set.toArray();
for(String str: arr){
    System.out.println(str)
}

正确的方式:

String[] arr = set.toArray(new String[set.size()]);
for(String str: arr){
    System.out.println(str)
}

Set,List转String数组

Set转String数组直接用其toArray()方法即可

注意事项如下

        Object[] array = set.toArray();
        // 错误!!!!!!!
        String[] targetFail = (String[]) array;
        错误信息如下:
ERROR 2019-06-04 17:43:13 [http-nio-9008-exec-7] com.wii.spring.framework.exception.GlobalExceptionResolver.exceptionHandler(GlobalExceptionResolver.java:106) - error:[Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
        at com.zjht.controller.OrderController.export(OrderController.java:457) ~[classes!/:0.0.1-SNAPSHOT]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_201]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_201]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_201]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_201]
        at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) ~[spring-web-5.0.12.RELEASE.jar!/:5.0.12.RELEASE]
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-5.0.12.RELEASE.jar!/:5.0.12.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.0.12.RELEASE.jar!/:5.0.12.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:891) ~[spring-webmvc-5.0.12.RELEASE.jar!/:5.0.12.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-5.0.12.RELEASE.jar!/:5.0.12.RELEASE]
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.0.12.RELEASE.jar!/:5.0.12.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) [spring-webmvc-5.0.12.RELEASE.jar!/:5.0.12.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) [spring-webmvc-5.0.12.RELEASE.jar!/:5.0.12.RELEASE]
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:981) [spring-webmvc-5.0.12.RELEASE.jar!/:5.0.12.RELEASE]
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:873) [spring-webmvc-5.0.12.RELEASE.jar!/:5.0.12.RELEASE]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) [tomcat-embed-core-8.5.37.jar!/:8.5.37]
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:858) [spring-webmvc-5.0.12.RELEASE.jar!/:5.0.12.RELEASE]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) [tomcat-embed-core-8.5.37.jar!/:8.5.37]                                                                                                                 

正确转换如下

private static void methodTwo(Set set) {
		Object[] array = set.toArray();
		// 错误!!!!!!!
		String[] targetFail = (String[]) array;
		for (String str : targetFail) {
			System.out.println("目标数组:" + str);
		}
		// 正确
		String[] targetSuccess = set.toArray(new String[set.size()]);
		for (String str : targetSuccess) {
			System.out.println("目标数组:" + str);
		}
	}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

你可能感兴趣的:(如何将Set直接转成数组)