Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Getting errors: TestGeneric.java:22: error: type ArrayList does not take paramet

ID: 663727 • Letter: G

Question

Getting errors:

TestGeneric.java:22: error: type ArrayList does not take parameters
public static<T> boolean isPalindrome(ArrayList<T> list)

TestGeneric.java:6: error: type ArrayList does not take parameters
ArrayList<String> aVal = new ArrayList<String>();

TestGeneric.java:6: error: type ArrayList does not take parameters
ArrayList<String> aVal = new ArrayList<String>();

TestGeneric.java:26: error: type ArrayList does not take parameters
ArrayList<T> al = new ArrayList<T>(list.subList(1, list.size()-1));

TestGeneric.java:26: error: type ArrayList does not take parameters
ArrayList<T> al = new ArrayList<T>(list.subList(1, list.size()-1));

JAVA: Provide a static method that checks whether a generic array list is a palindrome; that is, whether the values at index i and n - 1 - i are equal to each other, where n is the size of the array list.

My code:

import java.util.*;
public class TestGeneric
{
   public static void main(String args[])
   {
ArrayList<String> aVal = new ArrayList<String>();
aVal.add("lunch");
aVal.add("dinner");
if(isPalindrome (aVal))
{
   System.out.println("Word in the list is palindrome.");
}
else
{
   System.out.println("Word in the list is not a palindrome.");
}
Iterator itr1 = aVal.iterator();
while(itr1.hasNext()){
   System.out.println(itr1.next());
}
   }
   public static<T> boolean isPalindrome(ArrayList<T> list)
   {
if(list.size() == 0 || list.size() == 1)
   return true;
ArrayList<T> al = new ArrayList<T>(list.subList(1, list.size()-1));
return list.get(0).equals(list.get(list.size()-1)) && isPalindrome(al);
   }
}

Explanation / Answer

i suggest you :

1.The main class should be named as FilePalindromeChecker
2. you should Create a StringUtil class with a static method to check for palindrome: public static boolean checkPalindrome
3. you should have another static method in StringUtil to check for alphanumeric: public static boolean checkPalindrome

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Palindrome {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter starting point: ");
        int start = sc.nextInt();

        System.out.println("Enter finishing point: ");
        int finish = sc.nextInt();

        List<Integer> numbers = new ArrayList<Integer>();
        int count = 0;

        for (int i = start; i <= finish; i++) {
            numbers.add(i);
            if(isPalinDrome(i)){
                count++;
            }
        }

        System.out.println("[Start value: " + start + "] [End value: " + finish + "] [Number of PalinDromes: " + count + "]");

    }

    public static boolean isPalinDrome(int n){
        String str = Integer.toString(n);
        String reverse = new StringBuilder(str).reverse().toString();

        return str.equals(reverse);
    }

}

second one : you can gather ideas from this below program :

for (int i = 0; i < list.size(); i ++) {List<String> palindromeList = new ArrayList<String>();
    int wordLength = list.get(i);

    String leftSide = "";
    String rightSide ="";

    if (wordLength%2 == 1) { //If word has odd number of characters.
        leftSide = list.get(i).subString(0,wordLength/2);
        rightSide = list.get(i).subString((wordLength/2) + 1, wordLength);

    } else { //If word has even number of characters.
        leftSide = list.get(i).subString(0,(wordLength/2));
        rightSide = list.get(i).subString((wordLength/2), wordLength);
    }
    String reversedLeftSide = new StringBuilder(leftSide).reverse().toString();
    if (reversedLeftSide.equals(rightSide)) {
        palindromeList.add(list.get(i));
    }
}

String longestPalindrome = "";

//Searches for longest palindrome in the list of palindromes.

for (int i = 0; i < palindromeList.size(); i++) {
    if (palindromeList.get(i).length() > longestPalindrome.length()) {
        longestPalindrome = palindromeList.get(i);
    }
}

System.out.println(longestPalindrome); //This should give you longest palindrome

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote