Type int[][] of the last argument to method println(Object…) doesn't exactly match the vararg parameter type

huangapple go评论76阅读模式
英文:

Type int[][] of the last argument to method println(Object...) doesn't exactly match the vararg parameter type

问题

以下是您提供的Java代码的翻译部分:

static public void println(Object what) {
    if (what == null) {
        out.println("null");    
        out.flush();
    }
    else if (what.getClass().isArray()) {
        printlnArray(what);
    }
    else {
        out.println(what);
        out.flush();
    }
}

static public void println(Object... variables) {

    for (int i = 0; i < variables.length; i++) {
        Object o = variables[i];
        if (o.getClass().isArray()) {
            printlnArray(o);
        }
        else {
            out.print(o);
            if (i != variables.length-1) out.print(" ");
        }
    }
    out.println();
    out.flush();  
}

如果您有任何其他翻译需求,请告诉我。

英文:

I have the following:


static public void println(Object what) {
    if (what == null) {
        out.println(&quot;null&quot;);    
        out.flush();
    }
    else if (what.getClass().isArray()) {
        printlnArray(what);
    }
    else {
        out.println(what);
        out.flush();
    }
}


static public void println(Object... variables) {

    for (int i = 0; i &lt; variables.length; i++) {
        Object o = variables[i];
        if (o.getClass().isArray()) {
            printlnArray(o);
        }
        else {
            out.print(o);
            if (i != variables.length-1) out.print(&quot; &quot;);
        }
    }
    out.println();
    out.flush();  
}

If I call it like this:

int[][] array_2d = {{1}, {1, 2}, {1, 2, 3}};
println(&quot;array_2d&quot;);
println(array_2d);

Then I get the error:

> Type int[][] of the last argument to method println(Object...) doesn't exactly match the vararg parameter type. Cast to Object[] to confirm the non-varargs invocation, or pass individual arguments of type Object for a varargs invocation.

I can't seem to find a way to suppress this warning. Neither do I have any luck with fixing the error. Adding println(int[][] variables) is not an option.

I did an attempt in this direction:

enum Skip {
    SKIP
}

static public void println(Object[] variables) {
    println(Skip.SKIP, variables);
}

static public void println(Object first, Object... variables) {

    if (first instanceof Skip) {

    }
    else {
        print(first);
    }

But it does not work and would require to much obscure code to get it working.

Can this problem be fixed with reasonable clean code?

答案1

得分: 3

One problem is that invokes on println are ambiguous. After all, println(Object... x) is just syntax sugar for println(Object[] x) with the additional nicety that callers will silently create that array for you if invokes with multiple arguments. This makes invoking println with a single value, but that value is an array, ambiguous: Either form is plausible (an array is itself an Object!)

Simple solution is to disambiguate:

Now all forms except println() work out and are not ambiguous:

println(); // invokes #1 println(x); // invokes #2, and only #2. Even if x is an array. println(x, y); //invokes #3, with an empty array for &#39;rest&#39; println(x, y, a, b, c); // invokes #3.

NB: Do NOT write println(Object a, Object b) or println(Object a, Object... rest) - that would re-introduce ambiguity!

英文:

One problem is that invokes on println are ambiguous. After all, println(Object... x) is just syntax sugar for println(Object[] x) with the additional nicety that callers will silently create that array for you if invokes with multiple arguments. This makes invoking println with a single value, but that value is an array, ambiguous: Either form is plausible (an array is itself an Object!)

Simple solution is to disambiguate:

/*1*/ public void println() {}

/*2*/ public void println(Object o) { ... }

/*3*/ public void println(Object a, Object b, Object... rest) { }

Now all forms except println() work out and are not ambiguous:

println(); // invokes #1
println(x); // invokes #2, and only #2. Even if x is an array.
println(x, y); //invokes #3, with an empty array for &#39;rest&#39;
println(x, y, a, b, c); // invokes #3.

NB: Do NOT write println(Object a, Object b) or println(Object a, Object... rest) - that would re-introduce ambiguity!

答案2

得分: 2

警告出现是因为不清楚你想要做什么:

  • 将2D数组作为一个整体的Object传递给可变参数:

    // 为了更清晰,像调用非可变参数方法一样调用它
    println(new Object[] { array_2d })
    

    或者;

  • "展开"外部数组,使每个内部数组成为可变参数之一:

    println(new Object[] { array_2d[0], array_2d[1], array_2d[2] })
    

如果你想要前者,请将2D数组强制转换为Object。如果你想要后者,请将2D数组强制转换为Object[]

println((Object)array_2d);
// 或者
println((Object[])array_2d);

无论哪种方式都会消除警告。

英文:

The warning occurs because it is ambiguous whether you want to:

  • pass the 2D array as one whole Object to the varargs:

    // calling this as if it weren&#39;t a varargs method for clarity
    println(new Object[] { array_2d })
    

    or;

  • "splat" the outer array such that each inner array is one of the varargs:

    println(new Object[] { array_2d[0], array_2d[1], array_2d[2] })
    

If you want the former, cast the 2D array to Object. If you want the latter, cast the 2D array to Object[].

println((Object)array_2d);
// or
println((Object[])array_2d);

Doing either will silence the warning.

huangapple
  • 本文由 发表于 2020年7月28日 18:03:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63131631.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定