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.对象 需要: char
编辑:根据您的新编辑(包括错误消息(,这可能是您正在做的事情
Integer oneBoxed = 1;
char d = (char) oneBoxed; //error... required: char, found: Integer
你应该做的是
Integer oneBoxed = 1;
char d = (char) oneBoxed.intValue();
使用泛型时要小心,因为您被迫对所有基元进行装箱。
您是新来的,但在提问时请尝试发布更多信息;-(
---原答案---
我相对确定你正在尝试
- 将装箱
- 字符转换为装箱整数。或
- 将带框整数转换为带框字符。
要执行从字符到整数的转换,必须先将它们拆箱。
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!