You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java泛型方法传递数组参数报错?分析给定代码中的问题

Let's walk through the issues in this code—there are a couple of key problems preventing it from compiling, plus a minor style tweak worth noting:

1. Generics don't support primitive type arrays (major compilation error)

Your CountDuplicates method uses a generic type T extends Comparable<T> and expects a T[] array. Here's the catch: Java generics only work with reference types (objects), not primitive types like double or int.

When you pass D_arr (a double[]) or I_arr (an int[]) to this method, the compiler can't map these primitive arrays to a generic T[] because primitive types don't implement Comparable—only their wrapper classes (Double, Integer) do.

When you call CountDuplicates(D_arr, 0.0), 0.0 is a primitive double, but the method expects a T (which would need to be a Double object to align with a generic array). Similarly, passing the primitive 0 for the int[] call doesn't match the expected Integer type.

3. Minor style violation (not a compilation error)

Java coding conventions specify that method names should use camelCase (start with a lowercase letter). CountDuplicates starts with an uppercase letter—this won't break your code, but it's inconsistent with standard practice.

Fixed Code Options

Option 1: Use wrapper class arrays (stick with generics)

Replace the primitive arrays with their wrapper class equivalents, which work with generics:

class MaximumTest {
    // Renamed to follow camelCase convention
    public static <T extends Comparable<T>> int countDuplicates(T[] anArray, T elem) {
        int count = 0;
        for (T e : anArray) {
            if (e.compareTo(elem) == 0) {
                ++count;
            }
        }
        return count;
    }

    public static void main(String args[]) {
        // Use Double[] and Integer[] instead of primitive arrays
        Double[] D_arr = { 1.2, 3.4, 0.0, 4.5, 0.0 };
        Integer[] I_arr = { 0, 0, 0, 0, 1, 3, 5 };
        
        // Java auto-boxes primitive literals (0.0, 0) to wrapper objects here
        System.out.println("No of zeros in Double Array = " + countDuplicates(D_arr, 0.0));
        System.out.println("No of zeros in Integer Array = " + countDuplicates(I_arr, 0));
    }
}

Option 2: Overload methods for primitive types

If you want to keep using primitive arrays, you can create overloaded versions of the method for each primitive type (since generics can't handle them directly):

class MaximumTest {
    // Generic version for object arrays
    public static <T extends Comparable<T>> int countDuplicates(T[] anArray, T elem) {
        int count = 0;
        for (T e : anArray) {
            if (e.compareTo(elem) == 0) {
                ++count;
            }
        }
        return count;
    }

    // Overloaded version for double arrays
    public static int countDuplicates(double[] anArray, double elem) {
        int count = 0;
        for (double e : anArray) {
            if (e == elem) {
                ++count;
            }
        }
        return count;
    }

    // Overloaded version for int arrays
    public static int countDuplicates(int[] anArray, int elem) {
        int count = 0;
        for (int e : anArray) {
            if (e == elem) {
                ++count;
            }
        }
        return count;
    }

    public static void main(String args[]) {
        double[] D_arr = { 1.2, 3.4, 0.0, 4.5, 0.0 };
        int[] I_arr = { 0, 0, 0, 0, 1, 3, 5 };
        
        System.out.println("No of zeros in Double Array = " + countDuplicates(D_arr, 0.0));
        System.out.println("No of zeros in Integer Array = " + countDuplicates(I_arr, 0));
    }
}

内容的提问来源于stack exchange,提问作者Dr. Poonkavithai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:59:51