Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class MainClass {
  4.     public static void main(String[] args) {
  5.         int[] array = {1,2,3,4,5,4,3,2,1};
  6.         System.out.println(Arrays.toString(array));
  7.         System.out.println(_check(array));
  8.     }
  9.  
  10.     private static boolean _check(int[] array) {
  11.         boolean isPalindrome = true;
  12.         int length = array.length;
  13.  
  14.         int firstIndex = 0;
  15.         int lastIndex = array.length - 1;
  16.  
  17.         int formFirst = firstIndex;
  18.         int fromLast = lastIndex;
  19.  
  20.         while (formFirst <= length / 2
  21.                 && fromLast >= length / 2) {
  22.  
  23.             System.out.print(array[formFirst] + " " + array[fromLast] + " // ");
  24.             if(array[formFirst] != array[fromLast])
  25.                 isPalindrome = false;
  26.  
  27.             formFirst++;
  28.             fromLast--;
  29.         }
  30.         return isPalindrome;
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement