Java - Using Recursion to Find Palindrome -
i designing util class takes string parameter , returns true if palindrome(ex: input: radar ---> output: true) , returns false if not. class, using linkedlist, don't know why there seems error. here stack trace:
exception in thread "main" java.lang.indexoutofboundsexception: index: 4, size: 4 @ java.util.linkedlist.checkelementindex(linkedlist.java:555) @ java.util.linkedlist.remove(linkedlist.java:525) @ com.run.findpalindromes.findmain(findpalindromes.java:20) @ com.run.findpalindromes.findmain(findpalindromes.java:16) @ com.run.test.main(test.java:7) and here source code:
public boolean findmain(string in){ if(times == 0){ search = new linkedlist(cc.convertstringtoarraylist(in)); times ++; findmain(null); } else { if(search.get(search.size()-1).equals(search.get(0))){ search.remove(0); search.remove(search.size()); findmain(null); } else { system.out.println("not palindrome"); return false; } } return true; }
search.remove(search.size()) should search.remove(search.size() - 1) since lists zero-based. if have 4 elements list indices run 0 3, , there nothing @ location 4.
also code stands won't handle empty lists well, that's need check.
Comments
Post a Comment