Java program to check if given array is palindrome.

// © 2017 TheFlyingKeyboard
// theflyingkeyboard.net

public class CheckIfPalindrome {
  public static void main(String[] args) {
    Integer[] arr = { 1, 2, 3, 4, 5 };
    
    System.out.println(isPalindrome(arr));
  }
  
  public static <T> Boolean isPalindrome(T[] arrayToCheck){
    for ( int i = 0; i < arrayToCheck.length / 2; i++) {
        if (arrayToCheck[i] != arrayToCheck[arrayToCheck.length - 1 - i]) {
          return false;
        }
    }
    
    return true;
  }
}

 

Java Check If Array Is Palindrome
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *