String array manipulations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Take note of length - it is used as a String[] property > strings.length AND as a method for a String | |
array element > strings[i].length(). | |
*/ | |
public Map<String, String> pairs(String[] strings) { | |
Map<String,String> map = new HashMap<String,String>(); | |
for (int i = 0; i < strings.length; i++ ) { | |
if (strings[i].length() == 1 ) { | |
Character first = strings[i].charAt(0); | |
map.put(first.toString(), first.toString()); | |
} else { | |
// select indexes from the word | |
Character first = strings[i].charAt(0); | |
Character last = strings[i].charAt(strings[i].length() - 1); | |
map.put(first.toString(), last.toString()); | |
// converting the word to a char array | |
//char[] currentWord = strings[i].toCharArray(); | |
//Character first = currentWord[0]; | |
//int lastIndex = currentWord.length - 1; | |
//Character last = currentWord[lastIndex]; | |
//map.put( first.toString(), last.toString()); | |
} | |
} | |
return map; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment