<legend id='QxMsT'><style id='QxMsT'><dir id='QxMsT'><q id='QxMsT'></q></dir></style></legend>
  1. <i id='QxMsT'><tr id='QxMsT'><dt id='QxMsT'><q id='QxMsT'><span id='QxMsT'><b id='QxMsT'><form id='QxMsT'><ins id='QxMsT'></ins><ul id='QxMsT'></ul><sub id='QxMsT'></sub></form><legend id='QxMsT'></legend><bdo id='QxMsT'><pre id='QxMsT'><center id='QxMsT'></center></pre></bdo></b><th id='QxMsT'></th></span></q></dt></tr></i><div id='QxMsT'><tfoot id='QxMsT'></tfoot><dl id='QxMsT'><fieldset id='QxMsT'></fieldset></dl></div>
      • <bdo id='QxMsT'></bdo><ul id='QxMsT'></ul>

    1. <tfoot id='QxMsT'></tfoot>

      <small id='QxMsT'></small><noframes id='QxMsT'>

      Set toArray() 方法中需要 new String[0]

      Need for new String[0] in the Set toArray() method(Set toArray() 方法中需要 new String[0])
        • <i id='71k6n'><tr id='71k6n'><dt id='71k6n'><q id='71k6n'><span id='71k6n'><b id='71k6n'><form id='71k6n'><ins id='71k6n'></ins><ul id='71k6n'></ul><sub id='71k6n'></sub></form><legend id='71k6n'></legend><bdo id='71k6n'><pre id='71k6n'><center id='71k6n'></center></pre></bdo></b><th id='71k6n'></th></span></q></dt></tr></i><div id='71k6n'><tfoot id='71k6n'></tfoot><dl id='71k6n'><fieldset id='71k6n'></fieldset></dl></div>
            <bdo id='71k6n'></bdo><ul id='71k6n'></ul>
          • <legend id='71k6n'><style id='71k6n'><dir id='71k6n'><q id='71k6n'></q></dir></style></legend>

              <small id='71k6n'></small><noframes id='71k6n'>

                  <tbody id='71k6n'></tbody>
              • <tfoot id='71k6n'></tfoot>
                本文介绍了Set toArray() 方法中需要 new String[0]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试将 Set 转换为 Array.

                I am trying to convert a Set to an Array.

                Set<String> s = new HashSet<String>(Arrays.asList("mango","guava","apple"));
                String[] a = s.toArray(new String[0]);
                for(String x:a)
                      System.out.println(x);
                

                而且效果很好.但是我不明白 String[] a = s.toArray(new String[0]);.

                And it works fine. But I don't understand the significance of new String[0] in String[] a = s.toArray(new String[0]);.

                我的意思是一开始我在尝试 String[] a = c.toArray();,但它不起作用.为什么需要new String[0].

                I mean initially I was trying String[] a = c.toArray();, but it wan't working. Why is the need for new String[0].

                推荐答案

                如果足够大,就是要存放Set元素的数组;否则,将为此目的分配一个相同运行时类型的新数组.

                It is the array into which the elements of the Set are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.

                Object[] toArray(),返回一个 Object[] 不能转换为 String[] 或任何其他类型的数组.

                Object[] toArray(), returns an Object[] which cannot be cast to String[] or any other type array.

                T[] toArray(T[] a) ,返回一个包含该集合中所有元素的数组;返回数组的运行时类型是指定数组的运行时类型.如果集合适合指定的数组,则在其中返回.否则,使用指定数组的运行时类型和该集合的大小分配一个新数组.

                如果您通过实现代码(我从 OpenJDK) ,你会很清楚:

                If you go through the implementing code (I'm posting the code from OpenJDK) , it will be clear for you :

                 public <T> T[] toArray(T[] a) {
                     if (a.length < size)
                     // Make a new array of a's runtime type, but my contents:
                     return (T[]) Arrays.copyOf(elementData, size, a.getClass());
                     System.arraycopy(elementData, 0, a, 0, size);
                     if (a.length > size)
                         a[size] = null;
                    return a;
                 }
                

                这篇关于Set toArray() 方法中需要 new String[0]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Compiling C++ for the JVM(为 JVM 编译 C++)
                Compile to java bytecode (without using Java)(编译成java字节码(不使用Java))
                How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?(如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?)
                Java ClassLoader: load same class twice(Java ClassLoader:两次加载相同的类)
                How to debug .class files in ECLIPSE?(如何在 ECLIPSE 中调试 .class 文件?)
                Java quot;The blank final field may not have been initializedquot; Anonymous Interface vs Lambda Expression(Java“可能尚未初始化空白的最终字段匿名接口与 Lambda 表达式)
              • <legend id='Rn05r'><style id='Rn05r'><dir id='Rn05r'><q id='Rn05r'></q></dir></style></legend>
                  <tbody id='Rn05r'></tbody>

                    <bdo id='Rn05r'></bdo><ul id='Rn05r'></ul>

                    1. <i id='Rn05r'><tr id='Rn05r'><dt id='Rn05r'><q id='Rn05r'><span id='Rn05r'><b id='Rn05r'><form id='Rn05r'><ins id='Rn05r'></ins><ul id='Rn05r'></ul><sub id='Rn05r'></sub></form><legend id='Rn05r'></legend><bdo id='Rn05r'><pre id='Rn05r'><center id='Rn05r'></center></pre></bdo></b><th id='Rn05r'></th></span></q></dt></tr></i><div id='Rn05r'><tfoot id='Rn05r'></tfoot><dl id='Rn05r'><fieldset id='Rn05r'></fieldset></dl></div>

                      • <tfoot id='Rn05r'></tfoot>
                      • <small id='Rn05r'></small><noframes id='Rn05r'>