Search⌘ K

Solution Review: Is ArrayList a Palindrome?

Explore the process of checking if an ArrayList in Java is a palindrome by understanding the checkPalindrome function. Learn to iterate through elements, compare pairs from front and back, and control loop execution based on matching criteria. Gain practical skills in handling ArrayLists and implementing logic to verify palindrome sequences effectively.

Rubric criteria

Solution

Java
import java.util.ArrayList;
class Palindrome
{
public static void main(String args[])
{
ArrayList<String> str = new ArrayList<String>();
// Adding elements
str.add("m");
str.add("a");
str.add("d");
str.add("a");
str.add("m");
System.out.println(checkPalindrome(str));
}
public static boolean checkPalindrome(ArrayList<String> str)
{
if (str.size() == 0)
{
return true;
}
else
{
boolean cont = true; // Keep track whether it's plaindrome or not
for(int i = 0; i < str.size()/2 && cont; i++)
{
if (str.get(i) != str.get(str.size()-1-i)) // Comparing values
{
cont = false; // Match not found
}
}
if (cont)
return true;
else
return false;
}
}
}

Rubric-wise explanation


Point 1:

Look at the header of the checkPalindrome() function, at line 19. First, we check if the size of str is 00 ...