import java.util.Arrays; /** * Created by leon on 1/29/18. */ public class StringArrayUtils { /** * @param array array of String objects * @return first element of specified array */ // TODO public static String getFirstElement(String[] array) { String result = ""; result += array[0]; return result ; } /** * @param array array of String objects * @return second element in specified array */ public static String getSecondElement(String[] array) { String result = ""; result += array[1]; return result ; } /** * @param array array of String objects * @return last element in specified array */ // TODO public static String getLastElement(String[] array) { String result = ""; return array[array.length-1] ; } /** * @param array array of String objects * @return second to last element in specified array */ // TODO public static String getSecondToLastElement(String[] array) { String result = ""; return array[array.length-2]; } /** * @param array array of String objects * @param value value to check array for * @return true if the array contains the specified `value` */ // TODO public static boolean contains(String[] array, String value) { boolean x = false; for (String i : array){ if (i == value){ x = true; } } return x; } /** * @param array of String objects * @return an array with identical contents in reverse order */ // TODO public static String[] reverse(String[] array) { String order; for (int x = 0; x < array.length/2; x++){ order = array[x]; array[x] = array[array.length -x -1]; array[array.length -x -1] = order; } return array; } /** * @param array array of String objects * @return true if the order of the array is the same backwards and forwards */ // TODO public static boolean isPalindromic(String[] array) { boolean rev = false; for (int x = 0; x < array.length/2; x++){ if (array[x] == array[array.length -x -1]){ rev = true; } } return rev; } /** * @param array array of String objects * @return true if each letter in the alphabet has been used in the array */ // TODO public static boolean isPangramic(String[] array) { boolean every = false; StringBuilder build = new StringBuilder(); for(String s: array){ build.append(s); } String ofArr = ""; ofArr = build.toString(); for (char x = 'A'; x < 'Z'; x++){ if ((ofArr.indexOf(x) <0) && (ofArr.indexOf((char) (x + 32)) <0)){ every =false; } else{ every = true; } } return every; } /** * @param array array of String objects * @param value value to check array for * @return number of occurrences the specified `value` has occurred */ // TODO public static int getNumberOfOccurrences(String[] array, String value) { int occur = 0; for (int x = 0; x < array.length; x++){ if (value.equals(array[x])){ occur++; } } return occur; } /** * @param array array of String objects * @param valueToRemove value to remove from array * @return array with identical contents excluding values of `value` */ // TODO public static String[] removeValue(String[] array, String valueToRemove) { int place = 0; int count = getNumberOfOccurrences(array , valueToRemove); String[] newArr = new String [array.length - count]; for (int x = 0; x < array.length; x++){ if (array[x] != valueToRemove){ newArr[place] = array[x]; place++; } } return newArr; } /** * @param array array of chars * @return array of Strings with consecutive duplicates removes */ // TODO public static String[] removeConsecutiveDuplicates(String[] array) { int newArrIndex= 0; String[] newArr = new String[array.length]; newArr[newArrIndex] = array[0]; newArrIndex++; for (int x = 1; x < array.length; x++){ if (!array[x].equals(newArr[newArrIndex - 1])){ newArr[newArrIndex]= array[x]; newArrIndex++; } } String[] newArr2 = new String [newArrIndex]; System.arraycopy(newArr, 0 , newArr2, 0, newArrIndex); return newArr2; } /** * @param array array of chars * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings */ // TODO public static String[] packConsecutiveDuplicates(String[] array) { StringBuilder dups = new StringBuilder(array[0]); String[] newArr = new String[array.length-count(array)]; int adding = 0; for (int x = 1; x < array.length; x++){ if (array[x-1].equals(array[x])){ dups.append(array[x]); } else { newArr[adding] = dups.toString(); dups.setLength(0); dups.append(array[x]); adding++; } } newArr[adding] = dups.toString(); return newArr; } public static int count(String[] array){ int count = 0; for (int x = 1; x < array.length; x++){ if (array[x-1].equals(array[x])){ count++; } } return count; } }