1

I'm building a class that takes an array of numbers and has methods to output their min, max and average values as a string representation of the numbers. Here's my constructor for the class:

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);
       }
   }

I'm getting the array out of bounds exception message when I test this class with a driver program and this constructor is the one causing it. I'm not able to understand how I'm going beyond the size of the array here. Please help! Thanks.

Edit - So just to clear up any confusion I'm posting the entire class below for reference:

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;
   }

}
2
  • 2
    Where do you declare and initialize numbers? Commented Nov 26, 2015 at 22:58
  • the size of the array is a user input in the driver program. it has already been declared as part of the class. the numbers array has already been declared as a part of the class. and it would be initialized as the array is filled with random numbers generated in each iteration of the for loop. Commented Nov 26, 2015 at 23:02

3 Answers 3

1

You are initializing the array before you initialize the size variable. The size variable has a default value which is passed into the array constructor and sets the array to that size. to fix the problem just move the initialization of the array into the constructor after the size variable is set.

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);
   }
}

Also I've noticed a bug with the string output method. The array would be overwritten on each iteration. To solve this you must add the array to itself.

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;
}
Sign up to request clarification or add additional context in comments.

5 Comments

but then would the array be accessible to the whole class?
Yes it would because it is declared as a class variable rather than inside a method or constructor.
For the toStringArray method consider using a StringBuilder or simply replace it with Arrays.toString.
@Marvin Good call, I thought Array.toString would be a good way to go, but I didn't want to go to far a way from the original code.
Arrays.toString also uses a different string representation, which also might be an issue. Anyway, just wanted to mention it ;)
1

Your method is passed the size of the array, but the array definition is not present in your code.

If you need to create the array in the RandomArray method, do something like :

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;
   }

2 Comments

I've updated the entire class. What corrections do you think I can make?
What is the use of numbers[i] = generator.nextInt(size + 1) (versus numbers[i] = generator.nextInt(size + 1)) in the constructor ? Does your code compile ? You don't need two loops for min/max/avg functions, one is enough !
0

As you did not provide the entire class code it is hard to see whats going on. However I think you did not initialize the array correctly. If you have a private variable for an array, you should still 'make space' for it, as follows.

private int[] myIntArray; // As class member


myIntArray = new int[3]; // To allocate memory for the array

See the following link, for more info regarding arrays.

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

1 Comment

I've updated the entire class code. I believe I did initialize the array. What corrections can I make?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.