java类中构造函数中:数组怎样初始化

请给一个小例子
谢谢
在构造函数中给array初始化,如全部复值2

1、java构造函数--数组 在构造函数中初始化数组,如 public class array { private int [][] matrix; public array(int r,int c) { matrix=new matrix[r][c]; } }
matrix=new matrix[r][c];
2、这里是new int[][]; java中8个基本数据类型都是有默认值的,int默认值为0 ,
3、所以数组中默认都为0.
4、但是切记有默认值的变量必须是类的属性,方法中局部变量必须赋值才可以使用。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-12-03
数组一般都是用FOR循环初始化较多吧,如:
for(int i =0;i<array.length;i++){
array[i]=2;
}
第2个回答  2008-12-02
public class A{
int arr [];
public A(int arr[]){
this.arr = arr;
}

}

class B{
public void demo(){
int arr[] = {2,2,2,2,2,2};
new A(arr );
}
}本回答被网友采纳
第3个回答  2008-12-02
int arraySize = 10; // array size: 10 elements
int[] a = new int[arraySize ]; // declare and allocate memory
for(int i=0; i < arraySize; i++) { // initialization
a[i] = 2; // set every element to integer 2
}
第4个回答  2008-12-02
他们回答的差不多了
相似回答