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 中?

      时间:2023-10-14

      <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 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何获得两个地图Java之间的差异? 下一篇:如何找到集合的长度?

                相关文章

                <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>
              • <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>