ArrayList-based stack creation and use in the Palindrome detector

I need to create the myStack class and then use it to test Palindromes ... I decided to create an ArrayList-based stack implementation.

    import java.util.ArrayList;

    public class myStack<AnyType>{
        private ArrayList<AnyType> arr;

        public myStack(ArrayList<AnyType> a){
            arr = a;
        }

        public void push(AnyType element) {
                    arr.add(element);
        }

        public AnyType pop() {
            if(arr.size() == 0)
            {
                    System.out.println("Stack Underflow");
                    return null;
            }
            else
            {
                    AnyType element = arr.get(arr.size() -1);
                    arr.remove(element);
                    return element;
            }
        }

        public AnyType top() {
            if(arr.size() == 0)
            {
                    System.out.println("Stack Underflow");
                    return null;
            }
            else
                    return(arr.get(arr.size() -1));
        }
   }

Then I use it in the Palindrome class (not completed yet)

However, when I compile, this tells me Note: Palindrome.java uses unverified or insecure operations. Note. Recompiling with -Xlint: unchecked for details. "

When I comment out the line myStack m = new myStack (test);

It compiles, so I know this is a problem ... Why would myStack class be a problem? Is this somehow related to the "AnyType" that I used in the stack class?

+4
3

myStack. <AnyType> Raw Type ( ). Java myStack. ( Java 7 <>) -

// myStack m = new myStack(test);
myStack<Character> m = new myStack<>(); // <-- Please use MyStack.
+1

MyStack , "?".

, MyStack , , -.

0

... .

  • ?
  • Generics - / . . , , .. .
  • 3 - (a) , , (b) (c), ,
  • , , stackoverflow
  • , .

... Java , , Generics. :

  • - . StringUtil , checkPalindrome
  • ( Java )

:

  • Your main class should be called FilePalindromeChecker
  • Create a StringUtil class with a static palindrome check method: public static boolean checkPalindrome
  • Try another static method in StringUtil to check for an alphanumeric value: public static boolean checkPalindrome

Happy coding!

0
source

All Articles