-5
在构造函数中,我已经构建了一个数组,并且试图在该类的另一个函数中获取该数组的长度,但它不采用相同的数组,即使它的名称与数组相同是空的。我在一个类的构造函数中构造了一个数组,是否有一种方法可以让每个方法都能识别它?
在构造函数中,我已经构建了一个数组,并且试图在该类的另一个函数中获取该数组的长度,但它不采用相同的数组,即使它的名称与数组相同是空的。我在一个类的构造函数中构造了一个数组,是否有一种方法可以让每个方法都能识别它?
你的阵列应该是一个实例变量:
public class Example {
// Private variables defined outside of a method/constructor can
// be used anywhere within the class but not outside of the class
private String[] array;
public Example() {
array = new String[6];
}
public int length() {
return array.length;
}
}
哪里是你的代码? –