-1

I have a method that takes a char[][] as a parameter (basically a NxN grid of chars) and is utilizing an ArrayDeque in order to look through the whole 2d array. I want the row and column of a char[][] object removed from the ArrayDeque, and currently use this:

             ArrayDeque stack=new ArrayDeque();
             stack.push(grid[0][0]); //grid being the 2d array passed to the method
             char[][] temp=(char[][]) stack.pop(); 
             int row=temp.length-1;
             int column=temp[0].length-1;

This compiles in Eclipse, but when run throws a ClassCastException. Is there a way to get the row and column without the char[][] case in the second line above?

4

1 回答 1

1

ArrayDeque做什么?没有什么!您添加一个值,然后将其取回。

虽然,假设grid是 a char[][],并且知道push()需要 a Object,但它隐含地自动装箱该值。

简而言之,你得到的是:

temp = (char[][])Character.valueOf(grid[0][0])

您正在将 aCharacter转换为 a char[][],所以您当然会得到 a ClassCastException

于 2015-10-26T22:42:53.097 回答