user2908101
user2908101

Reputation: 99

Convert object type to integer

i'm having trouble solving this problem.

Lets say I have a Stack<P> board = new Stack[3][3]; and I initialized each row and column to have an empty stack.

And lets see I pushed a P object onto board[1][2].push(P). I know that I'm able to access the top of this stack using the simple method board[1][3].peek(); This would return the top element with the type P. This is where the problem is.

I'm told to write a function called...

public int getTopPiece(int row, int col) which returns an integer with the value of that top object called P. Any advice?

   P is an object that holds 2 integers, 1 indicating the value of the piece and the second the value of the player.

Upvotes: 2

Views: 310

Answers (2)

SDLeffler
SDLeffler

Reputation: 585

There's a crucial piece you're leaving out here. What you don't give us is what P is and how to convert it to an integer. If you're going to write a function called getTopPiece(int row, int col) and you want it to return the integer value of a P object, then first you need a way to get an integer value of a P object, perhaps through a P.getIntegerValue() function. If you have a getIntegerValue function in your P class, then you can do this:

public int getTopPiece(int row, int col)
{
    return board[row][col].peek().getIntegerValue();
}

Otherwise I don't think there's any way we can help. You need to have a way to confidently convert P to an integer. Unless P is an instanceof Integer - in which case you can simply do

public int getTopPiece(int row, int col)
{
    return board[row][col].peek();
}

since (Assuming you're using Java) Integer can be converted automatically straight to int.

EDIT: Noting that you now have a piece value, it can be written as (assuming you want to peek and not to remove the element:)

public int getTopPiece(int row, int col)
{
    return board[row][col].peek().piece;
}

Although, I would like to ask: is there a reason you're using Stack? Do you want pieces to be able to stack on top of each other? If that's the case, then is there a limit to how many pieces you want to stack? Because if that's so, you'd want to use an array instead. But is there no limit? Because if so, then as already stated you'd want to use an ArrayDeque

.

Upvotes: 2

Makoto
Makoto

Reputation: 106508

I'm not entirely sure what you're type-bound to, but...

If you have a Stack<P>, you can only ever push or pop a type P from that. From this, we diverge into two possibilities:

  • If P is a generic type, stop using it, and use Integer instead.

    Stack<Integer> board = new Stack[3][3];
    

    Then, you can push and pop without having to do type casts.

  • If P is a custom class, pop it and extract the integer value from it.

    P value = board[0][0].pop(); // or peek(), whichever use is necessary.
    // with value, call whichever getter is appropriate.
    

In either case, writing the method should be straightforward - consider that row and col correspond directly to accessing the array.

board[row][col] // gives you the Stack you want

Now, to go on a completely different tangent, you really shouldn't be using Stack. Look into Deque instead.

Deque<P> board = new ArrayDeque[3][3];

You'll have access to the same interface of push, pop, and peek, without the synchronization overhead that Vector (and thus, Stack) would give you.

Upvotes: 2

Related Questions