我正在构建一个带有数组数组的类,并且有方法输出它们的最小值,最大值和平均值作为数字的字符串表示形式。这是我班级的构造函数:
public RandomArray(int sizeOfArray)/*Constructor: gets array size and populates array with
random numbers*/
{
Random generator = new Random();
size = sizeOfArray;
for (int i = 0;i < size;i++)
{
numbers[i] = generator.nextInt(size + 1);
}
}
当我使用驱动程序测试此类时,我正在获取数组超出范围的异常消息,并且此构造函数是导致它的那个。我无法理解我如何超越数组的大小。请帮忙!感谢。
编辑 - 所以只是为了澄清任何混淆我在下面发布整个课程以供参考:
public class RandomArray
{
/*A class that contains an array of random numbers and methods that output
the numbers' minimum, maximum and average values. Also includes a method
that outputs a string representation of the numbers.*/
int size, min, max;
String array;
int[] numbers = new int[size];
public RandomArray(int sizeOfArray)/*Constructor: gets array size and populates array with
random numbers*/
{
Random generator = new Random();
size = sizeOfArray;
for (int i = 0;i < size;i++)
{
numbers[i] = generator.nextInt(size + 1);
}
}
public int min_value()
{
for (int i = 0;i < size - 1;i++)
{
min = numbers[i];
for (int k = 1;k < size; k++)
{
if (numbers[k] < min)
{
min = numbers[k];
}
else
{
min = numbers[i];
}
}
}
return min;
}
public int max_value()
{
for (int i = 0;i < size - 1;i++)
{
max = numbers[i];
for (int k = 1;k < size; k++)
{
if (numbers[k] > max)
{
max = numbers[k];
}
else
{
max = numbers[i];
}
}
}
return max;
}
public double average()
{
double avg;
int sum = 0;
for (int i = 0;i < size;i++)
{
sum = sum + numbers[i];
}
avg = sum/size;
return avg;
}
public String toStringArray()//Outputs a string representation of all the numbers in the array
{
for (int i = 0; i < size;i++)
{
array = Integer.toString(numbers[i]) + " ";
}
return array;
}
}
答案 0 :(得分:1)
您的方法传递的是数组的大小,但代码中不存在数组定义。
如果需要在RandomArray方法中创建数组,请执行以下操作:
public int[] RandomArray(int sizeOfArray)/*Constructor: gets array size, create and populates array with random numbers*/
{
int[] randomArray = new int[sizeOfArray];
for (int i = 0;i < randomArray.length();i++)
{
numbers[i] = generator.nextInt(size + 1);
}
return randomArray;
}
答案 1 :(得分:1)
在初始化size变量之前,您正在初始化数组。 size变量具有默认值,该值传递给数组构造函数并将数组设置为该大小。修复问题只需在设置size变量后将数组的初始化移动到构造函数中。
public class RandomArray
{
/*A class that contains an array of random numbers and methods that output
the numbers' minimum, maximum and average values. Also includes a method
that outputs a string representation of the numbers.*/
int size, min, max;
String array;
int[] numbers;
public RandomArray(int sizeOfArray) {
Random generator = new Random();
size = sizeOfArray;
numbers = new int[size];
for (int i = 0;i < size;i++)
{
numbers[i] = generator.nextInt(size + 1);
}
}
此外,我还注意到了字符串输出方法的错误。每次迭代都会覆盖该数组。要解决此问题,您必须将数组添加到自身。
public String toStringArray()//Outputs a string representation of all the numbers in the array
{
for (int i = 0; i < size;i++)
{
array = array + Integer.toString(numbers[i]) + " ";
}
return array;
}
答案 2 :(得分:0)
由于您没有提供整个班级代码,因此很难看到最新情况。但是我认为你没有正确初始化数组。如果你有一个数组的私有变量,你仍然应该为它“腾出空间”,如下所示。
private int[] myIntArray; // As class member
myIntArray = new int[3]; // To allocate memory for the array
有关阵列的更多信息,请参阅以下链接。
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html