I've implemented a method to reverse a String using stack as ArrayList, but an error says error: incompatible types: Object cannot be converted to char
. How can I fix it?
Here is the stack class:
public static class stackl extends ArrayList {
public boolean isEmptyS() {
return isEmpty();
}
public void push(Object el) {
add(0, el);
}
public Object pop() {
return remove(0);
}
}
And here is my method:
public static void reverse(String s) {
stackl s1 = new stackl();
for (int j = 0; j < s.length(); j++) {
char ch = s.charAt(j);
s1.push(ch);
}
while (!s1.isEmpty()) {
char ch = s1.pop();
System.out.println(ch);
}
}
You can convert a char
to a Character
with auto-boxing. Try
Character ch = s.charAt(j);
Also, it would make sense to have a generic stack just like the built in Stack
(have a look at the code for Stack as an example)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments