如何修复此VarArgs构建错误?

时间:2018-04-26 17:16:52

标签: java variadic-functions

这是主要方法:

class coinbuilder(self, currency, currencyLong, minConfirmation...):
    def __init__(self):
        self.currency = currency
        self.currencyLong = currencyLong
    ticker = requests.get("https://bittrex.com/api/v1.1/public/getticker?market=BTC-" + currency)

这是var args方法:

package main;

import varArgs.VarArgs;

public class Main {

    public static void main(String[] args) {
        int answer;

        answer = VarArgs.sum(new int[]{1,2,3});
        System.out.println("sum of ints = " + answer);

        answer = VarArgs.sum(new int[]{1,2,3}, new int[] {100, 200, 300});
        System.out.println("sum of ints = " + answer);

    }
}

这是我得到的错误: Error

1 个答案:

答案 0 :(得分:2)

varargs参数只能接受单个数组。如果要传入可变数量的数组,则需要执行以下操作:

public static int sum(int[]... arrays) {
    int sum = 0;
    for (int[] numbers : arrays) {
        for (int i : numbers) {
            sum += i;
        }
    }
    return sum;
}