Solution Review: Find if the Array is a Palindrome
This lesson gives a detailed solution review of how to find if the array is a palindrome.
We'll cover the following...
Solution: Is the array a palindrome?
Press + to interact
import java.io.*;class ChallengeClass {private static boolean palindrome(int[] array, int idx) {if (idx == array.length/2)return true;else if (array[idx] != array[array.length-1-idx])return false;elsereturn palindrome(array, idx+1);}public static void main(String[] args) {System.out.println("Array: ");//int[] array = {6,7,8,7,6};int[] array = {1,2,2,2,1};System.out.print("{ ");for (int i = 0; i < array.length; i++) {System.out.print(array[i] + ", ");}System.out.println("} ");System.out.println("Is it a palindrome?");Boolean result = palindrome(array, 0);System.out.println(result);}}
Understanding the code
The recursive code can be broken down into two parts: the recursive method and the main where the method is called.
Driver Method
The driver code is between line 15 and line 29
- An
array
is defined. - The method
palindrome
is called which takes two arguments - thearray