In C, I could initialize 2d char array using something like
public static char hashTable[10][5] = {"", "", "abc", "def", "ghi", "jkl",
"mno", "pqrs", "tuv", "wxyz"};
I am wondering how to do this in Java?
You can invoke toCharArray()
on your String
literals, the most similar construct I can think of is
public static char[][] hashTable = {
"".toCharArray(), "".toCharArray(),
"abc".toCharArray(), "def".toCharArray(),
"ghi".toCharArray(), "jkl".toCharArray(),
"mno".toCharArray(), "pqrs".toCharArray(),
"tuv".toCharArray(), "wxyz".toCharArray()
};
You can do the following,
public static char[][] arr = {{'a', 'd'},{'s','w','f'}};
arr[0] references the first array which is {'a', 'd'} while arr[1] references the second
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.