Java varags在另一个vararg方法中调用vararg方法时没有检测到

时间:2015-09-08 08:08:40

标签: java methods variadic-functions

我已经实现了Java方法来解决IN和OR条件。

以下是我的代码。

public static <T> boolean in(T parameter, T... values) {
    if (null != values) {
        System.out.println("IN values size.. " + values.length);
        return Arrays.asList(values).contains(parameter);
    }
    return false;
}

public static boolean or(boolean... values) {
    System.out.println("OR values size.. " + values.length);
    return in(true, values);
}

public static void main(String[] args) {
    System.out.println(or(false, true, false));
}

输出是:

OR values size.. 3
IN values size.. 1
false

但我期待以下输出:

OR values size.. 3
IN values size.. 3
true

我不明白为什么varargs在in方法中的大小为1。

3 个答案:

答案 0 :(得分:5)

方法

in(T parameter, T... values) //in(true, values); // values is T

当您传递布尔数组values时,整个数组将作为单个元素T,这是显示它1的原因。

您正在传递布尔数组,接收类型为T,其中每个元素都被视为数组。

您可以在方法中打印值,看看结果是什么。烈&#39;看一个数组对象。不是单独的布尔元素。

答案 1 :(得分:2)

当您输入or时,boolean... values参数会转换为boolean数组。然后,当您调用in(true, values)时,in的第二个参数实际上是基本类型boolean的数组(因此是单个值)。实际问题是Java不会自动封装基本类型的数组。

public static boolean or(boolean... values) {
    System.out.println("OR values size.. " + values.length);
    // here values is an array of the primitive boolean
    return in(true, values);
}

public static void main(String[] args) {
    System.out.println(or(false, true, false));
}

您可以将boolean装入这样的Boolean对象来解决此问题:

public static <T> boolean in(T parameter, T... values) {
    if (null != values) {
        System.out.println("IN values size.. " + values.length);
        return Arrays.asList(values).contains(parameter);
    }
    return false;
}

public static boolean or(boolean... values) {
    System.out.println("OR values size.. " + values.length);
    Boolean[] boxedValues = new Boolean[values.length];
    for (int i = 0; i < values.length; i++) {
        boxedValues[i] = values[i];
    }
    return in(true, boxedValues);
}

public static void main(String[] args) {
    System.out.println(or(false, true, false));
}

请注意,从Java 7开始,此代码将发出警告,您可以使用@SafeVarargs注释禁用。

答案 2 :(得分:0)

我使用静态工具来处理这种奇怪的边缘情况。

/**
 * Can rebox a boxed primitive array into its Object form.
 *
 * Generally I HATE using instanceof because using it is usually an indication that your hierarchy is completely wrong.
 *
 * Reboxing - however - is an area I am ok using it.
 *
 * Generally, if a primitive array is passed to a varargs it is wrapped up as the first and only component of an Object[].
 *
 * E.g.
 *
 * public void f(T... t) {}; f(new int[]{1,2});
 *
 * actually ends up calling f with t an Object[1] and t[0] the int[].
 *
 * This unwraps it and returns the correct reboxed version.
 *
 * In the above example it will return an Integer[].
 *
 * Any other array types will be returned unchanged.
 *
 * @author OldCurmudgeon
 */
public static class Rebox {

    public static <T> T[] rebox(T[] it) {
        // Default to return it unchanged.
        T[] result = it;
        // Special case length 1 and it[0] is primitive array.
        if (it.length == 1 && it[0].getClass().isArray()) {
            // Which primitive array is it?
            if (it[0] instanceof int[]) {
                result = rebox((int[]) it[0]);
            } else if (it[0] instanceof long[]) {
                result = rebox((long[]) it[0]);
            } else if (it[0] instanceof float[]) {
                result = rebox((float[]) it[0]);
            } else if (it[0] instanceof double[]) {
                result = rebox((double[]) it[0]);
            } else if (it[0] instanceof char[]) {
                result = rebox((char[]) it[0]);
            } else if (it[0] instanceof byte[]) {
                result = rebox((byte[]) it[0]);
            } else if (it[0] instanceof short[]) {
                result = rebox((short[]) it[0]);
            } else if (it[0] instanceof boolean[]) {
                result = rebox((boolean[]) it[0]);
            }
        }
        return result;
    }

    // Rebox each one separately.
    private static <T> T[] rebox(int[] it) {
        T[] boxed = makeTArray(it.length);
        for (int i = 0; i < it.length; i++) {
            boxed[i] = (T) Integer.valueOf(it[i]);
        }
        return boxed;
    }

    private static <T> T[] rebox(long[] it) {
        T[] boxed = makeTArray(it.length);
        for (int i = 0; i < it.length; i++) {
            boxed[i] = (T) Long.valueOf(it[i]);
        }
        return boxed;
    }

    private static <T> T[] rebox(float[] it) {
        T[] boxed = makeTArray(it.length);
        for (int i = 0; i < it.length; i++) {
            boxed[i] = (T) Float.valueOf(it[i]);
        }
        return boxed;
    }

    private static <T> T[] rebox(double[] it) {
        T[] boxed = makeTArray(it.length);
        for (int i = 0; i < it.length; i++) {
            boxed[i] = (T) Double.valueOf(it[i]);
        }
        return boxed;
    }

    private static <T> T[] rebox(char[] it) {
        T[] boxed = makeTArray(it.length);
        for (int i = 0; i < it.length; i++) {
            boxed[i] = (T) Character.valueOf(it[i]);
        }
        return boxed;
    }

    private static <T> T[] rebox(byte[] it) {
        T[] boxed = makeTArray(it.length);
        for (int i = 0; i < it.length; i++) {
            boxed[i] = (T) Byte.valueOf(it[i]);
        }
        return boxed;
    }

    private static <T> T[] rebox(short[] it) {
        T[] boxed = makeTArray(it.length);
        for (int i = 0; i < it.length; i++) {
            boxed[i] = (T) Short.valueOf(it[i]);
        }
        return boxed;
    }

    private static <T> T[] rebox(boolean[] it) {
        T[] boxed = makeTArray(it.length);
        for (int i = 0; i < it.length; i++) {
            boxed[i] = (T) Boolean.valueOf(it[i]);
        }
        return boxed;
    }

    // Trick to make a T[] of any length.
    // Do not pass any parameter for `dummy`.
    // public because this is potentially re-useable.
    public static <T> T[] makeTArray(int length, T... dummy) {
        return Arrays.copyOf(dummy, length);
    }
}

public static <T> boolean in(T parameter, T... values) {
    if (null != values) {
        System.out.println("IN values size.. " + values.length);
        return Arrays.asList(values).contains(parameter);
    }
    return false;
}

public static boolean or(boolean... values) {
    System.out.println("OR values size.. " + values.length);
    return in(true, Rebox.rebox(values));
}

public void test() {
    System.out.println(or(false, true, false));
}

打印:

OR values size.. 3
IN values size.. 3
true

根据您的要求。