简体   繁体   English

在具有不同数据类型的 java 中初始化二维数组

[英]Initializing 2d array in java having different data types

I want to initialize a 2d array in java which can hold different data types like char, int etc. My 2d array would be something like this -我想在 java 中初始化一个二维数组,它可以保存不同的数据类型,如 char、int 等。我的二维数组将是这样的 -

  1 2 3 4 5 6 7
1 S S S S S S S
2 S S S S S S S
3 S S S S S S S
4 S S S S B S S
5 S S S S S S S
6 S S S S S B S
7 S S S S S S S

There is a space before 1 and I want to include that in my array.If I can initialize that, how can I do that? 1 之前有一个空格,我想将它包含在我的数组中。如果我可以初始化它,我该怎么做? Do I use ASCII convention while inputting the string values or something?输入字符串值或其他内容时是否使用 ASCII 约定? Please help.请帮忙。 Note - I don't want to print the matrix, I will input these values manually.注意 - 我不想打印矩阵,我将手动输入这些值。

I see two solutions if you want to use a single primitive type in the array, either treat the numbers as characters so you can have an array of char如果您想在数组中使用单个原始类型,我会看到两种解决方案,或者将数字视为字符,这样您就可以拥有一个 char 数组

char[][] array2d = new char[8][8]

or simply ignore the numbers and only include the characters and deduct the right number from the index (x,y) in the array或者干脆忽略数字,只包含字符并从数组中的索引 (x,y) 中减去正确的数字

char[][] array2d = new char[7][7]

so for array2d[3][5] the numbers would be 4 and 6所以对于 array2d[3][5] 数字将是 4 和 6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM