如何将Object转为int?
本来是很简单的问题 但是用在linkQueue就行不通了 并报错
Exception in thread "main"
java.lang.NumberFormatException: For input string: "OneLinkNode@1fb8ee3"
at
java.lang.NumberFormatException.forInputString(Unknown Source)
下面是我写的
public class Queue_ex {
public static void main(String args[]){
LinkQueue ex=new LinkQueue();
ex.inQueue(new Integer(1));
ex.inQueue(new Integer(2));
Object k=ex.outQueue();
int s = Integer.parseInt(k.toString());
System.out.print(s);
}
}
public class LinkQueue {
//private OneLinkNode head;
private OneLinkNode front;// 队首指示器
private OneLinkNode rear;// 队尾指示器
private int s;// 队列标识位
public LinkQueue()//n表示链表的和长度
{
front = new OneLinkNode();//初始化头结点 最后head是null
rear = front;
}
public boolean inQueue(Object k) {//链表肯定不会满 所以不用考虑这一情况
OneLinkNode p = new OneLinkNode(k);
rear.next = p;
rear= rear.next;
s++;
return true;
}
public boolean isEmpty()
{
return s == 0;
/*这句话相当于
* if(head.next==null)
return true;
else
return false;*/
}
public boolean isFull() {
return false;
}
public int length()
{
return s;
}
public Object outQueue() {
Object k;
OneLinkNode p = front.next;
k=p;
front.next=front.next.next;
s--;
return k;
}
public void setEmpty() {
front=rear;
s=0;
}
public void output()
{
OneLinkNode p=front.next;
while(p!=null)
{
System.out.print(p.data);
p = p.next;
if(p!=null)
System.out.print("->");
}
System.out.println();
}
}
但是这样又可以
public class Test {
public static void main(String args[]) {
Object k;
k=new Integer(2);
int s=Integer.parseInt(k.toString());
System.out.print(s);
}
}
------解决方案--------------------
Java code
public Object outQueue() {
Object k;
OneLinkNode p = front.next;
k=p; /*这里K=P,你的k类型为OneLinkNode,在main函数中转换成Integer时应该用OneLinkNode.data来获得存入的Object.否则该句改为k=p.data*/
front.next=front.next.next;
s--;
return k;
}