【问题标题】:Create a Return Method which accepts an int array and returns ordered and a non-duplicated int array in java创建一个返回方法,它接受一个 int 数组并在 java 中返回有序和非重复的 int 数组
【发布时间】:2020-11-02 06:01:55
【问题描述】:

给定和 int 数组; int[] arr = {1, 2, 5, 8, 9, 10, 20, 80, 9, 0, 100, 90, 30, 55, 22, 87, 88, 22, 33, 22, 1, 2, 3};

要求: 要求是:有一个数组,没有排序和重复。创建了一个可以接受 int[] 的方法,并在对重复项进行排序和删除后,将其作为 int[] 返回...

我的处理方案是:

static void removingDups4(int[] arr) {

    LinkedHashSet<Integer> setDups = new LinkedHashSet(Arrays.asList(arr));

    for (int each : arr) {
        setDups.add(each);
    }

    System.out.println(setDups);

    int[] newArr = new int[setDups.size()];
    int i = 0;
    while (i < setDups.size()) {

        for (int each : setDups) {
            newArr[i] +=  each;
            i++;
        }
        System.out.println(newArr);

    }

}

输出:

[[I@77459877, 0, 1, 2, 3, 5, 8, 9, 10, 20, 22, 30, 33, 55, 80, 87, 88, 90, 100] Exception in thread "main" java.lang.ClassCastException: class [I cannot be cast to class java.lang.Integer ([I and java.lang.Integer are in module java.base of loader 'bootstrap')    at SearchingAnElementFromArray_BinarySearch.removingDups4(SearchingAnElementFromArray_BinarySearch.java:86)     at Array.SearchingAnElementFromArray_BinarySearch.main(SearchingAnElementFromArray_BinarySearch.java:12)

Process finished with exit code 1

【问题讨论】:

标签: java arrays linkedhashset


【解决方案1】:

这是一种方法(需要 Java8+):

public static int[] sortIntArrayNoDups(int[] array) {
    int[] tmp = java.util.stream.IntStream.of(array).distinct().toArray();
    Arrays.sort(tmp);
    return tmp;
}

使用方法:

int[] arr = {1, 2, 5, 8, 9, 10, 20, 80, 9, 0,
             100, 90, 30, 55, 22, 87, 88, 22, 
             33, 22, 1, 2, 3};
arr = sortNoDups(arr);
System.out.println(Arrays.toString(arr));

输出:

[0, 1, 2, 3, 5, 8, 9, 10, 20, 22, 30, 33, 55, 80, 87, 88, 90, 100]

【讨论】:

  • 如果先调用 .distinct() 再排序呢?如果有很多重复,那么排序会花费很多不必要的时间。
  • 你也可以return IntStream.of(array).distinct().sorted().toArray();
  • 查看comment by saka1029中的代码运行live at IdeOne.com
【解决方案2】:

使用 Java-8:

static int[] removingDups4(int[] arr) {
    return 
        new ArrayList<>(
            Arrays
            .stream(arr)       // Generating a stream of the `int` values held in the array.
            .boxed()           // Auto-boxing `int` primitives to `Integer` objects.
            .collect( Collectors.toCollection(TreeSet::new) )  // Passing the `Integer` objects into a `TreeSet` to (a) eliminate duplicates, and (b) sort them.
        )
        .stream()
        .mapToInt(i -> i)
        .toArray();
}

基本上将数字放入Set 将删除重复项。

并且使用SortedSet/NavigableSet 之类的TreeSet 也会对数字进行排序。

看到这个code run live at IdeOne.com

        int[] input = { 8 , 6 , 7 , 5 , 3, 0 , 9 , 8 };  // Repeating `8` at beginning and end. 
        int[] result = removingDups4( input ) ;
        System.out.println( Arrays.toString( result ) ) ;

[0, 3, 5, 6, 7, 8, 9]

【讨论】:

    【解决方案3】:

    您可以使用 TreeSet 代替 LinkedHashSet,它按升序对元素进行排序并自动删除重复项。 然后,您可以使用 toArray() 方法将 TreeSet 转换为对象数组,然后将其转换为整数数组并返回。 我没有把代码放在这里,因为它很容易弄清楚。

    【讨论】:

    • static Integer[] orderingAnArrayAndRemovingDups2(int[] arr) { TreeSet set = new TreeSet(Arrays.asList(arr)); Integer[] newArr = new Integer[set.size()]; set.toArray(newArr);返回新的Arr; }。我试过这种方式,但仍然有一些例外。像:线程“main”中的异常java.lang.ClassCastException:类[我无法转换为类java.lang.Comparable([我和java.lang.Comparable在加载器'bootstrap'的模块java.base中)跨度>
    猜你喜欢
    • 2021-11-01
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    相关资源
    最近更新 更多