【发布时间】:2011-11-25 16:45:48
【问题描述】:
我正在尝试实现一个涉及堆栈数组的程序。每个堆栈都接受 Integer 对象,但问题是当我尝试从堆栈中获取 Integer 对象时:
import java.util.*;
public class Blocks
{
public static void main(String[] args)
{
System.out.println();
Scanner input = new Scanner(System.in);
Stack[] blocks = new Stack[input.nextInt()];
for (int i = 0; i < blocks.length; i++) {blocks[i] = new Stack<Integer>();} //initializing main array of stacks of blocks
for (int i = 0; i < blocks.length; i++) {blocks[i].push(i);} //add first block to each stack
Stack retainer = new Stack<Integer>(); //used for when moving stacks of blocks instead of one block.
boolean m; //move or pile
boolean on; //onto or over
int fromBlock; //block being moved
int toBlock; //block where the fromBlock is being moved
String command = input.next();
while (!command.equals("quit"))
{
m = command.equals("move");
fromBlock = input.nextInt();
on = input.next().equals("onto");
toBlock = input.nextInt();
if (m) //put back blocks on fromBlock
{
if (on) //put back blocks on toBlock
{
int holder = blocks[fromBlock].pop().intValue(); //I get a compiler error here
moveOnto(blocks, holder, toBlock);
}
else //fromBlock goes on top of stack on toBlock
{
}
}
else //bring blocks on fromBlock
{
if (on) //put back blocks on toBlock
{
}
else //fromBlock goes on top of stack on toBlock
{
}
}
command = input.next();
}
}
void moveOnto(Stack[] array, int sBlock, int rBlock)
{
}
}
错误表示无法识别 .intValue()。显然这是 Integer 的一种方法,从那时我发现它返回的是 Object 对象而不是 Integer 类型。我怎样才能让它返回整数类型?
【问题讨论】: