计算给定数组中的重复项数

输入:1,4,2,6,7,5,1,2 输出:2


计算java给定数组中重复数字的数量。我首先对数组进行排序,然后计算重复项。它向我显示了未使用变量 c 并且此方法应返回 int 值的错误。


public class Duplicates

public static void main(String[] args) {

    int[]list;

    int[]c;

        int[] c = new int[list.length];

        int temp;

        for (int i = 0; i < list.length - 1; i++) {

            for (int j = i + 1; j < list; j++) {

                if (list[I] > list[j]) {

                    temp = list[i];

                    list[i] = list[j];

                    list[j] = temp;

                    c = list;

                }

            }

        }

        int n = 0;

        int counter = 0;

        int a = -1;

        for (int i = 0; i < c.length; ++i) {

            if (c[i] == a) {

                ++n;

                if (n == 1) {

                    ++counter;

                    if (counter == 1) {

                        System.out.print(c[i]);

                    } else {

                        System.out.print("," + c[i]);

                    }

                }

            } else {

                a = c[i];

                n = 0;

            }

        }

        System.out.println("\nNumber of Duplicated Numbers in array:" + counter);

    }

}


梵蒂冈之花
浏览 79回答 1
1回答

MYYA

它向我显示未使用变量 c 的错误这应该是一个警告。因此,即使显示此代码,代码仍应正确运行。此方法应返回 int 的值这是一个编译错误,因为您没有int在方法末尾返回任何数组,所以您的方法的返回类型应该是void. 你应该改变你的方法签名如下,public static void c(int[] list)int否则,您将需要在方法结束时返回一个数组。修复代码后,public class Duplicates {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; int[] list = new int[]{1, 4, 2, 6, 7, 5, 1, 2};&nbsp; &nbsp; &nbsp; &nbsp; int temp;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < list.length; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 1; j < (list.length - i); ++j) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (list[j - 1] > list[j]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp = list[j - 1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list[j - 1] = list[j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list[j] = temp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; int n = 0, counter = 0;&nbsp; &nbsp; &nbsp; &nbsp; int previous = -1;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < list.length; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (list[i] == previous) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++n;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (n == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++counter;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (counter == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(list[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(", " + list[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; previous = list[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("\nNumber of Duplicated Numbers in array: " + counter);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java