堆栈返回对象而不是整数 [英] Stack returning Objects instead of Integers

查看:31
本文介绍了堆栈返回对象而不是整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个涉及堆栈数组的程序.每个堆栈都接受 Integer 对象,但问题是当我尝试从堆栈中获取 Integer 对象时:

I'm trying to implement a program that involves an array of stacks. Each stack takes in Integer objects, but the problem is when I try to get an Integer object from the stack:

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 类型.我怎样才能让它返回整数类型?

The error says that is doesn't recognize .intValue(). Obviously that is a method of Integer, and I found out from that point that it's returning Object objects instead of Integer types. How can I make it return Integer types?

推荐答案

(哎呀 - Java 不允许泛型数组)更改您的 Stack 变量声明以使用 Stack 的通用版本:

(oops - Java doesn't allow arrays of generics) Change your Stack variable declaration to use the generic version of Stack:

Stack<Integer>[] blocks = new Stack<Integer>[input.nextInt()];

否则,当您访问非通用堆栈的 .pop() 方法时,它只会返回一个对象,您需要将其转换回整数 为了访问 Integer 方法,例如intValue():

Otherwise when you access the non-generic stack's .pop() method it just returns an Object which you would need to cast back to an Integer In order to access Integer methods like intValue():

int holder = ((Integer)blocks[fromBlock].pop()).intValue();

(但如果您修复了声明,则不需要强制转换.)

(But you won't need to cast if you fix the declarations.)

这篇关于堆栈返回对象而不是整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆