如何将int数组作为参数传递给构造函数? [英] How to pass int array as an argument to a constructor?

查看:59
本文介绍了如何将int数组作为参数传递给构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我是否有可能将int数组作为参数传递给构造函数?

Could someone tell me if its possible to pass an int array as an argument to a constructor?

我尝试了以下操作:

public static void main (String args[]){

    Accumulator[] X = new Accumulator[3];     

}

public Accumulator(int[] X) {
    A= new int[X.length];
    for (int i=0; i<X.length; i++)
        A[i] = X[i];
}

推荐答案

您正在初始化main方法中大小为3的累加器数组.

You are initializing an array of Accumulators with size three in your main method.

要将int数组传递给Accumulator的构造函数,您需要执行以下操作:

To pass an int array to the constructor of Accumulator you would need to do something like the following:

public static void main (String args[]){
    int[] someArray = {1,2,3};

    Accumulator accumulator = new Accumulator(someArray);
}

public Accumulator(int[] X) {
    A= new int[X.length];
    for (int i=0; i<X.length; i++)
        A[i] = X[i];
}

这篇关于如何将int数组作为参数传递给构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆