1

Is it possible to create a 2d array in java, such the column and row names are letter-characters and we can access like array[E][*] = ... ?

Here we can't use map because Map corresponds to 1-D array. So what will be the other alternatives ?

3
  • Possible duplicate of Java: Create an array with letter characters as index Commented Feb 23, 2017 at 3:40
  • You can use a 26 X 26 array and index it using the value (letter - 'a') if it is a lowercase letter, and (letter -'A') if it is an uppercase letter. Commented Feb 23, 2017 at 3:40
  • I need to use special characters too. Commented Feb 23, 2017 at 3:46

1 Answer 1

0

As you said in the comments that Map corresponds to 1D array, why not use Map of Maps.

Map<Character,Map<Character,Character>> outerMap = new HashMap<Character, Map<Character, Character>>();

and use a simple method to fetch your data:

public char getValue(char row ,char column, Map<Character,Map<Character,Character>> outerMap) {

return  outerMap.get(row).get(column);

}

Good Luck!

Sign up to request clarification or add additional context in comments.

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.