This repository has been archived on 2026-05-20. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
CSE-123/in_class/cse123-inclass9/IsPalindromeArray.java
2026-05-20 16:20:45 -07:00

26 lines
988 B
Java

public class IsPalindromeArray {
public static void main(String[] args) {
System.out.println(isPalindrome(new int[] { 1, 2, 3, 4, 5, 4, 3, 2, 1 }));
System.out.println(isPalindrome(new int[] { 10, 20, 20, 10 }));
System.out.println(isPalindrome(new int[] { 1 }));
System.out.println(isPalindrome(new int[] {}));
System.out.println(isPalindrome(new int[] { 1, 2, 3, 4, 5 }));
System.out.println(isPalindrome(new int[] { 1, 2, 5, 3 }));
System.out.println(isPalindrome(new int[] { 10, 20, 30, 10 }));
}
public static boolean isPalindrome(int[] array) {
return isPalindrome(array, 0, array.length - 1);
}
private static boolean isPalindrome(int[] array, int lower, int upper) {
if (upper - lower < 1) {
return true;
} else if (array[lower] != array[upper]) {
return false;
} else {
return isPalindrome(array, lower+1,upper-1);
}
}
}