-3
public static Node stringToList(String s) {
    Node<Character> head = new Node<Character>();
    for(int i = 0; i < s.length(); i++) {
    head = new Node<Character>(s.charAt(i), head);
    }
    char c = head.item;      // error line
}



I am trying to convert the char value in the nodes to an integer value and I believe I have found a way to do so. Just want to store the item in nodes to variable c for the time being. And then I got the error message. 
Why am I getting an incompatible types error? And it says "required: character".

// Node class:
public class Node<Item> {
    public Item item;           //as you can see, it a generic class
    public Node<Item> next;
    Node() { 
        item = null; 
        next = null;      
    } 
    Node(Item i) {
        item = i;
        next = null;
    }
    Node(Item i, Node<Item> p) {
        item = i;
        next = p;
    }
}

完整的错误消息:文件:/Users/Michelle/BigInt.java [行:11] 错误:/Users/Michelle/BigInt.java:11:找到不兼容的类型:java.lang.Object required: char

4

1 回答 1

0

编辑:根据您的新编辑,包括错误消息,这可能是您正在做的

Integer oneBoxed = 1;
char d = (char) oneBoxed; //error... required: char, found: Integer 

你应该做的是

Integer oneBoxed = 1;
char d = (char) oneBoxed.intValue();

使用泛型时要小心,因为您被迫将所有原语装箱。

您是新来的,但请在提问时尝试发布更多信息;-)

---原始答案---

我相对确定你正在尝试

  1. 将盒装字符转换为盒装整数。或者
  2. 将盒装整数转换为盒装字符。

要执行从 Character 到 Integer 的转换,您必须先将它们拆箱。

Integer one = 1;
Character b = one;
    //Error will be
    //
    //required: Character
    //found: Integer

另一种方式

Character c = 'c';
Integer cInt =  c;
    //Error will be
    //
    //required: Integer
    //found: Character

要正确转换这些值,您需要先将 rValue 拆箱。

Integer one = 1;
Character b = (char) one.intValue();
//Yay! No Error

Character c = 'c';
Integer cInt = (int) c.charValue();
//Yay! No Error Again!
于 2013-10-09T06:23:19.107 回答