Skip to content

Instantly share code, notes, and snippets.

@infomaven
Created December 20, 2016 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save infomaven/783c39cbddaf65c5a4c261d9c9ab9ae0 to your computer and use it in GitHub Desktop.
Save infomaven/783c39cbddaf65c5a4c261d9c9ab9ae0 to your computer and use it in GitHub Desktop.
String array manipulations
/*
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