[英]Error: incompatible types: Object cannot be converted to char
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
. 我已经实现了一种使用堆栈作为ArrayList来反转String的方法,但是错误提示
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. 您可以将转换
char
的Character
用的自动装箱。 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) 同样,像内置
Stack
一样具有通用堆栈也是有意义的(以Stack
的代码为例)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.