简体   繁体   中英

How to declare a multidimensional array variable with two or more single array in java?

Is it possible to use many simple array values to declare as multidimensional array values?

char[] alpha1 = {"A","B","C","D","E"};
char[] alpha2 = {"F","G","H","I","J"};
//Is it possible? Give me a solution for this situation: 
char[][] alpha = { alpha1[] , alpha2[] };

With " " you declare a String. Use ' ' for a character.

But yes it is possible, but you have to remove [] :

char[] alpha1 = {'A','B','C','D','E'};
char[] alpha2 = {'F','G','H','I','J'};
char[][] alpha = { alpha1 , alpha2 };

There are a couple of ways to declare multidimensional array:

  1. char[][] alpha = {{'A','B','C','D','E'}, {'F','G','H','I','J'}};
  2. char[][] alpha = new char[4][]; alpha[0] = new char[3]; alpha[1] = new char[2];

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