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

      • <bdo id='t5lxA'></bdo><ul id='t5lxA'></ul>
    1. <legend id='t5lxA'><style id='t5lxA'><dir id='t5lxA'><q id='t5lxA'></q></dir></style></legend>
    2. <small id='t5lxA'></small><noframes id='t5lxA'>

      如何正确地将数组添加到 Set 中?

      How to add an Array into Set properly?(如何正确地将数组添加到 Set 中?)

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

        <tbody id='l2slU'></tbody>
    3. <legend id='l2slU'><style id='l2slU'><dir id='l2slU'><q id='l2slU'></q></dir></style></legend>

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

        <tfoot id='l2slU'></tfoot>

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

                本文介绍了如何正确地将数组添加到 Set 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试将整数数组添加到 Set 中,如下所示,

                I'm trying to add in Integer array into Set as following,

                int[] arr = { 2, 6, 4 , 2, 3, 3, 1, 7 }; 
                Set<Integer> set = new HashSet<Integer>(Arrays.asList(arr));
                

                我收到一些错误提示,如下所示,

                I'm getting some error telling as following,

                myTest.java:192: error: no suitable constructor found for HashSet(List<int[]>)
                    Set<Integer> set = new HashSet<Integer>(Arrays.asList(arr));
                                       ^
                constructor HashSet.HashSet(Collection<? extends Integer>) is not applicable
                  (argument mismatch; inferred type does not conform to upper bound(s)
                      inferred: int[]
                      upper bound(s): Integer,Object)
                constructor HashSet.HashSet(int) is not applicable
                  (argument mismatch; no instance(s) of type variable(s) T exist so that List<T> conforms to int)
                 where T is a type-variable:
                T extends Object declared in method <T>asList(T...)
                Note: Some messages have been simplified; recompile with -Xdiags:verbose to        get full output
                   1 error
                

                其次,我也尝试了以下,但仍然出现错误,

                Secondly, I also tries as following and still getting error,

                int[] arr = { 2, 6, 4 , 2, 3, 3, 1, 7 }; 
                Set<Integer> set = new HashSet<Integer>( );
                Collections.addAll(set, arr);
                

                如何在 Java 中正确地将整数数组添加到 Set 中?谢谢.

                How to add an Integer array into Set in Java properly ? Thanks.

                推荐答案

                需要使用wrapper类型才能使用Arrays.asList(T...)

                You need to use the wrapper type to use Arrays.asList(T...)

                Integer[] arr = { 2, 6, 4, 2, 3, 3, 1, 7 };
                Set<Integer> set = new HashSet<>(Arrays.asList(arr));
                

                手动添加元素,如

                int[] arr = { 2, 6, 4, 2, 3, 3, 1, 7 };
                Set<Integer> set = new HashSet<>();
                for (int v : arr) {
                    set.add(v);
                }
                

                最后,如果需要保留插入顺序,可以使用 LinkedHashSet.

                Finally, if you need to preserve insertion order, you can use a LinkedHashSet.

                这篇关于如何正确地将数组添加到 Set 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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 表达式)
                <i id='7DLwa'><tr id='7DLwa'><dt id='7DLwa'><q id='7DLwa'><span id='7DLwa'><b id='7DLwa'><form id='7DLwa'><ins id='7DLwa'></ins><ul id='7DLwa'></ul><sub id='7DLwa'></sub></form><legend id='7DLwa'></legend><bdo id='7DLwa'><pre id='7DLwa'><center id='7DLwa'></center></pre></bdo></b><th id='7DLwa'></th></span></q></dt></tr></i><div id='7DLwa'><tfoot id='7DLwa'></tfoot><dl id='7DLwa'><fieldset id='7DLwa'></fieldset></dl></div>
                  <tbody id='7DLwa'></tbody>

                <small id='7DLwa'></small><noframes id='7DLwa'>

                • <legend id='7DLwa'><style id='7DLwa'><dir id='7DLwa'><q id='7DLwa'></q></dir></style></legend>
                    <bdo id='7DLwa'></bdo><ul id='7DLwa'></ul>

                        1. <tfoot id='7DLwa'></tfoot>