从2d对象数组中获取int数组

时间:2022-12-20 21:34:52

So I am creating a tile game and I save each tile as an int array. I represent the board with a 2d object array and then I save the 2d object array into a file using object output stream.

我创建了一个tile game,我将每个tile保存为int数组。我用2d对象数组表示棋盘,然后使用对象输出流将2d对象数组保存到文件中。

I can get the value of the object array, but not the int array stored in it. I need the integers stored in the integer array.

我可以得到对象数组的值,但不能得到存储在其中的int数组。我需要整数数组中存储的整数。

public Start()
{
    Object[][] tileBoard = WorldIO.loadWorld( "A player named file");
    int[] singleTile = tileBoard[0][0]; //the error
}

It is asking me if I want to cast it to int[], but that still does not work.

它问我是否要将它转换为int[],但这仍然不起作用。

1 个解决方案

#1


0  

Due to your code, I am assuming we are dealing with Integers and tileBoard is a 2D array of Integers.

由于你的代码,我假设我们正在处理整数和tileBoard是一个二维数组的整数。

When you wrote tileBoard[0][0], the type that is returned is an Integer and you are trying to save that Integer as an Integer array, therefore the types do not match. Try doing int [] array = 5 for example, you will get an error.

当您编写tileBoard[0][0]时,返回的类型是一个整数,您试图将该整数保存为一个整数数组,因此类型不匹配。尝试做int [] array = 5,例如,你会得到一个错误。

  • If what you wanted to do was to save that single integer at tileBoard[0][0] in an array, all you have to do is int [] singleTile = {tileBoard[0][0]};By adding the curly braces, you are now saving an integer inside an integer array.

    如果您想要做的是将该整数保存在一个数组的tileBoard[0][0]中,那么您所需要做的就是将int [] singleTile = {tileBoard[0][0]}保存;

  • If what you wanted to do was to extract an entire array from the 2D array and save that array into your singleTile object, you should use a for loop.

    如果您想要做的是从2D数组中提取整个数组并将该数组保存到singleTile对象中,那么应该使用for循环。

Here is what you can do

这是你能做的。

int lengthOfRow = tileBoard[0].length;//Getting the length of the row at row 0
int [] singleTile = new int[lengthOfRow]//Defining length of your new array
for(int i = 0; i < lengthOfRow; i++){
     int add = tileBoard[0][i];
     singleTile[i] = add;//Adding the elements to your single array
}

If you think of a 2D array as a grid with rows and columns, what you can do is pick a row and move through it using a for loop (using a pen and paper will help you visualize it). Since your new array will be identical to a row inside your 2D array, they must have the same length. First you have to get that length and initiate your new array accordingly. Then you loop over the columns, int add is first at tileBoard[0][0] then tileBoard[0][1] then tileBoard[0][2], until there are no more elements in that row.

如果你把二维数组想象成一个有行和列的网格,你能做的就是选择一个行并使用for循环(使用笔和纸可以帮助你形象化它)在其中移动。由于您的新数组将与2D数组中的一行相同,因此它们必须具有相同的长度。首先你需要得到这个长度并相应地初始化你的新数组。然后对这些列进行循环,int add首先是在tileBoard[0][0]然后是tileBoard[0][1]然后是tileBoard[0][2],直到这一行不再有元素。

#1


0  

Due to your code, I am assuming we are dealing with Integers and tileBoard is a 2D array of Integers.

由于你的代码,我假设我们正在处理整数和tileBoard是一个二维数组的整数。

When you wrote tileBoard[0][0], the type that is returned is an Integer and you are trying to save that Integer as an Integer array, therefore the types do not match. Try doing int [] array = 5 for example, you will get an error.

当您编写tileBoard[0][0]时,返回的类型是一个整数,您试图将该整数保存为一个整数数组,因此类型不匹配。尝试做int [] array = 5,例如,你会得到一个错误。

  • If what you wanted to do was to save that single integer at tileBoard[0][0] in an array, all you have to do is int [] singleTile = {tileBoard[0][0]};By adding the curly braces, you are now saving an integer inside an integer array.

    如果您想要做的是将该整数保存在一个数组的tileBoard[0][0]中,那么您所需要做的就是将int [] singleTile = {tileBoard[0][0]}保存;

  • If what you wanted to do was to extract an entire array from the 2D array and save that array into your singleTile object, you should use a for loop.

    如果您想要做的是从2D数组中提取整个数组并将该数组保存到singleTile对象中,那么应该使用for循环。

Here is what you can do

这是你能做的。

int lengthOfRow = tileBoard[0].length;//Getting the length of the row at row 0
int [] singleTile = new int[lengthOfRow]//Defining length of your new array
for(int i = 0; i < lengthOfRow; i++){
     int add = tileBoard[0][i];
     singleTile[i] = add;//Adding the elements to your single array
}

If you think of a 2D array as a grid with rows and columns, what you can do is pick a row and move through it using a for loop (using a pen and paper will help you visualize it). Since your new array will be identical to a row inside your 2D array, they must have the same length. First you have to get that length and initiate your new array accordingly. Then you loop over the columns, int add is first at tileBoard[0][0] then tileBoard[0][1] then tileBoard[0][2], until there are no more elements in that row.

如果你把二维数组想象成一个有行和列的网格,你能做的就是选择一个行并使用for循环(使用笔和纸可以帮助你形象化它)在其中移动。由于您的新数组将与2D数组中的一行相同,因此它们必须具有相同的长度。首先你需要得到这个长度并相应地初始化你的新数组。然后对这些列进行循环,int add首先是在tileBoard[0][0]然后是tileBoard[0][1]然后是tileBoard[0][2],直到这一行不再有元素。