0

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.

4
  • 2
    Do you really need a 2D array, or will map[1].charAt(1) suffice? Commented Feb 27, 2013 at 1:27
  • Definetely needs to be a 2D array unfortunatly. Commented Feb 27, 2013 at 1:28
  • Do you need Strings[][], or char[][] will also do the trick? Commented Feb 27, 2013 at 1:30
  • Well it's in a String, but a char[][] would be better, I just don't know how to Commented Feb 27, 2013 at 1:30

2 Answers 2

3

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"]
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.