简体   繁体   English

错误:类型不兼容:对象无法转换为char

[英]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. 您可以将转换charCharacter用的自动装箱。 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.

相关问题 错误:类型不兼容:char无法转换为String - error: incompatible types: char cannot be converted to String 错误:不兼容的类型:char[] 无法转换为 char (java) - Error: incompatible types: char[] cannot be converted to char (java) 错误:不兼容的类型:对象无法转换为树 - error: incompatible types: Object cannot be converted to Trees 有关不兼容类型的错误:字符串无法转换为char - error about incompatible types: String cannot be converted to char 不兼容的类型:char[] 无法转换为 CharSequence - incompatible types: char[] cannot be converted to CharSequence 错误:(32,27)错误:不兼容的类型:对象无法转换为long - Error:(32, 27) error: incompatible types: Object cannot be converted to long Java错误,类型不兼容:无法将对象转换为字符串 - Java Error, Incompatible types: Object cannot be converted to string StringTokenizer错误:类型不兼容:无法将对象转换为String - StringTokenizer error: incompatible types: Object cannot be converted to String 错误:类型不兼容:java.lang.Object 无法转换为 E - Error: incompatible types: java.lang.Object cannot be converted to E 不兼容的类型:对象不能转换为另一个对象? - Incompatible types: Object cannot be converted to another Object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM