简体   繁体   English

带有二维数组的Java varargs

[英]Java varargs with 2-dimensional arrays

Question is left here because people answered it, my problem was that the version of the API I was using was out of sync with the docs I had....You can in fact do this. 问题留在这里,因为人们回答了它,我的问题是我使用的API的版本与我的文档不同步....你实际上可以做到这一点。

Is there any way to use a 2-d array in Java as an argument for an argument that expects a vararg of arrays? 有没有办法在Java中使用二维数组作为参数的参数,该参数需要一个数组变量?

The function I am trying to call is 我试图打电话的功能是

public Long sadd(final byte[] key, final byte[]... members) {

and I have a 2-d array of bytes(byte [][] data=blah) 我有一个2-d字节数组(byte [] [] data = blah)

however if I try to call 但是,如果我试着打电话

sadd(key,data);

I get the following compiler error: 我得到以下编译器错误:

(actual argument byte[][] cannot be converted to byte[] by method invocation conversion) (实际参数byte [] []不能通过方法调用转换转换为byte []

Is there any way to use a 2-d array as a vararg of an array type? 有没有办法使用二维数组作为数组类型的变量?

The following works for me. 以下适用于我。 Perhaps you're not doing what you think you're doing? 也许你没有做你认为你在做的事情?

@Test
public void test_varargs() {
   byte[] x = new byte[] { 1, 2, 3};
   byte[] y = new byte[] { 0, 1, 2};
   assertEquals(9L, sum(x,y));
   byte[][] z = new byte[][] { x,y };
   assertEquals(9L, sum(z));
}

public long sum(final byte[]... members) {
   long sum = 0;
   for (byte[] member : members) {
       for (byte x : member) {
         sum += x;
      }
   }
   return sum;
}

Can you provide more of your code because this compiles for me. 你可以提供更多的代码,因为这可以为我编译。

byte[][] data = new byte[1][];
byte[] key = new byte[1];

long sadd = sadd(key, data);
class A {
    void show(int[] ax,int[]...arr) {
        for (int is : ax) {
            System.out.println(is);
        }
        for (int[] a : arr) {
            for (int i : a) {
                System.out.println(i);
            }
        }
    }
}
public class abc{
    public static void main(String[] args) {
        A a = new A();
        int[] arr1= new int[]{10,20};
        int[][] arr2 = new int[][] { { 10, 20 }, { 20, 20 }, { 30, 20 } };
        a.show(arr1,arr2);  
    }
}

Here I have used 2-d array as var args parameter and a 1-d array as fixed parameter. 这里我使用2-d数组作为var args参数,使用1-d数组作为固定参数。 Refer this code if this can help you! 如果这可以帮助您,请参考此代码! :) :)

It's not possible since the compiler has no way to infer the two dimensions. 这是不可能的,因为编译器无法推断出这两个维度。 When using one-dimensional array you can determine the length of the array as the number of auxiliary arguments (those that are not mandatory). 使用一维数组时,您可以将数组的长度确定为辅助参数的数量(非必需参数)。

eg: Let's say you method definition includes n mandatory parameters and, at runtime, you supply m more arguments. 例如:假设你的方法定义包括n强制性参数,并在运行时,需要提供m更多的参数。 Those m arguments are going to make up the array of auxiliary arguments. 那些m参数将构成辅助参数的数组。 The length is m . 长度是m In case of a two-dimensional array, the compiler has to come up with two dimensions for the array such that: dimension1 * dimension2 = m . 在二维数组的情况下,编译器必须为数组提出两个维度: dimension1 * dimension2 = m

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM