So, I was trying to code some test cases for a Java project I'm doing, and I decided that I wanted to move them into a function of their own, and choose between them using a parameter of the test function, rather than just using commenting and uncommenting of code, as I'm currently doing. However, I hit a snag: the code in question involves the initialisation of array variables, and if I try to initialise them inside an if-else statement, then the other pieces of code later on aren't capable of seeing them due to scope issues. Additionally, in Java, arrays are of a fixed size, and some of the test cases involve arrays of different sizes (including empty arrays), so simply creating the array before hand doesn't work.
Here's the code I've already tried (with arrayGen being a function that creates an integer array of n elements, with pseudorandom values that lie between the lower and upper bounds, inclusive). Note that in order to change which test case I'm running, I need to comment out the current test case and uncomment the test case I want to run; I'd like to replace this with a series of if-else if-else statements if possible.
System.out.println("Unsorted:");
int[] unsorted = arrayGen(n,lower,upper);
//sorted array:
//int[] unsorted = new int[n];
//for (int i=0;i<n;i++)
//{
// if (lower+i<upper)
// {
// unsorted[i]=lower+i;
// }
// else unsorted[i]=upper;
//}
//reverse sorted array:
//int[] unsorted = new int[n];
//for (int i=0;i<n;i++)
//{
// if (upper-i>lower)
// {
// unsorted[i]=upper-i;
// }
// else unsorted[i]=lower;
//}
//array of static numbers:
//int[] unsorted = arrayGen(n,upper,upper);
//empty array:
//int[] unsorted = arrayGen(0,lower,upper);
Is there any way to fix this, and have a neater version of my code, or am I going to have to be stuck with just commenting and uncommenting the test cases I want to use?