hunterge
hunterge

Reputation: 653

String Array to 2D String array

I have a String Array, map[] which looks like...

"####"

"#GB#"

"#BB#"

"####"

So map[1] = "#GB#"

How do I turn this into a 2D array so that newMap[1][1] would give me "G"?

Thanks a lot.

Upvotes: 0

Views: 333

Answers (2)

Karthik T
Karthik T

Reputation: 31952

If you really need it, you can use String.toCharArray on each element array to convert them into an array.

String[] origArr = new String[10];


char[][] charArr = new char[10][];

for(int i = 0; i< origArr.length;i++)
    charArr[i] = origArr[i].toCharArray();

If you want to break it up into String[] instead, you could use (thanks Pshemo)

String[] abc = "abc".split("(?!^)"); //-> ["a", "b", "c"]

Upvotes: 3

roguequery
roguequery

Reputation: 974

This won't be dynamic. It will take O(n) + m to get to a character of a string. A much faster and dynamic approach would be a Hashmap where the key is the String and the value is a char array. Kind of unnecessarily complex but you get the seeking and individual letter charAts without having to go through the cumbersome process of resizing a primitive array.

Upvotes: 0

Related Questions