简体   繁体   中英

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 -

  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? Do I use ASCII convention while inputting the string values or something? 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[][] 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

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

so for array2d[3][5] the numbers would be 4 and 6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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