I\'m having trouble with this code. The code compiles, but whenever I run the pr
ID: 3760437 • Letter: I
Question
I'm having trouble with this code. The code compiles, but whenever I run the program, it inevitably crashes and I receive this error message: java.lang.StackOverflowError:null.
I'm trying to recursively reverse an array in Java. My code is below. (I prompt for input in my Main and assign the input to the array.)
public class ReverseArray
{
public static void main (String[]args)
{
Scanner input = new Scanner(System.in);
int size;
System.out.println();
System.out.println ("Enter the number of elements in your array: ");
size = input.nextInt();
int[] myArray = new int[size];
System.out.println();
System.out.println ("Enter the elements of your array: ");
for (int i = 0; i < size; i++)
{
myArray[i] = input.nextInt();
}
System.out.println();
System.out.println ("Array: ");
for (int i = 0; i < size; i++)
{
System.out.println (myArray[i] + " ");
}
System.out.println();
arrayReversal(myArray, 0, myArray.length - 1);
System.out.println ("Array reversed: ");
printArray(myArray, 6);
System.out.println();
}
static void arrayReversal(int[] array, int first, int last)
{
int temp;
if (first >= last + 1)
{
return;
} else
{
temp = array[first];
array[first] = array[last];
array[last] = temp;
arrayReversal(array, first++, last--);
}
}
Please help.
Explanation / Answer
//Changes are in bold letters . Errors fixed in program
//Program prints the array elements in reverse order
//ReverseArray.java
import java.util.Scanner;
public class ReverseArray
{
public static void main (String[]args)
{
Scanner input = new Scanner(System.in);
int size;
System.out.println();
System.out.println ("Enter the number of elements in your array: ");
//read size
size = input.nextInt();
int[] myArray = new int[size];
System.out.println();
System.out.println ("Enter the elements of your array: ");
for (int i = 0; i < size; i++)
{
myArray[i] = input.nextInt();
}
System.out.println();
System.out.println ("Array: ");
for (int i = 0; i < size; i++)
{
System.out.println (myArray[i] + " ");
}
System.out.println();
//calling recursive method
arrayReversal(myArray, 0,myArray.length-1);
System.out.println ("Array reversed: ");
/**
* Error Fixed:
* Pass the argument size read by user.
* So that method array prints the values of given size
* */
printArray(myArray, size);
System.out.println();
}
/**The method printArray that takes an array of type integers
* and size of the array as input arguments and prints
* the array of given size value*/
private static void printArray(int[] myArray, int size)
{
for (int i = 0; i < size; i++)
{
System.out.println(myArray[i]);
}
}
/**Reverse the array elements calling recursive */
static void arrayReversal(int[] array, int first, int last)
{
int temp;
if (first >= last + 1)
{
return;
}
else
{
temp = array[first];
array[first] = array[last];
array[last] = temp;
/*Error Fixed :
* Calling method arrayReversal by adding 1 to first and subtracting
* value one from last.
* Do not apply incrementation or decrementation
* on first and last variables while calling recursive methods.
* In method recursive methods, incrementation or decrementions
* forms infinite loops with new first and last values
* */
arrayReversal(array, first+1, last-1);
}
}
}
------------------------------------------------------------
Sample Output:
Enter the number of elements in your array:
5
Enter the elements of your array:
1
2
3
4
5
Array:
1
2
3
4
5
Array reversed:
5
4
3
2
1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.