我已经用ArrayList作为stack来实现了一个字符串反转的方法,但是出现了错误 error: incompatible types: Object cannot be converted to char
。我该如何解决?
以下是stack类:
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);
}
}
以下是我的方法:
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);
}
}