如何將Set直接轉成數組

Set直接轉成數組

例子

public class Test{
    
    public void test1(){
        Set<String> 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<String> 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);
		}
	}

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: