StringArrayUtils.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import java.util.Arrays;
  2. /**
  3. * Created by leon on 1/29/18.
  4. */
  5. public class StringArrayUtils {
  6. /**
  7. * @param array array of String objects
  8. * @return first element of specified array
  9. */ // TODO
  10. public static String getFirstElement(String[] array) {
  11. return array[0];
  12. }
  13. /**
  14. * @param array array of String objects
  15. * @return second element in specified array
  16. */
  17. public static String getSecondElement(String[] array) {
  18. return array[1];
  19. }
  20. /**
  21. * @param array array of String objects
  22. * @return last element in specified array
  23. */ // TODO
  24. public static String getLastElement(String[] array) {
  25. return array[array.length-1];
  26. }
  27. /**
  28. * @param array array of String objects
  29. * @return second to last element in specified array
  30. */ // TODO
  31. public static String getSecondToLastElement(String[] array) {
  32. return array[array.length-2];
  33. }
  34. /**
  35. * @param array array of String objects
  36. * @param value value to check array for
  37. * @return true if the array contains the specified `value`
  38. */ // TODO
  39. public static boolean contains(String[] array, String value) {
  40. boolean output = false;
  41. for (String test: array){
  42. if (test.equals(value)){
  43. output = true;
  44. break;
  45. }
  46. }
  47. return output;
  48. }
  49. /**
  50. * @param array of String objects
  51. * @return an array with identical contents in reverse order
  52. */ // TODO
  53. public static String[] reverse(String[] array) {
  54. String[] newArray = new String[array.length];
  55. for (int start = 0; start < array.length; start++){
  56. newArray[start] = array[array.length-start-1];
  57. }
  58. return newArray;
  59. }
  60. /**
  61. * @param array array of String objects
  62. * @return true if the order of the array is the same backwards and forwards
  63. */ // TODO
  64. public static boolean isPalindromic(String[] array) {
  65. boolean output = false;
  66. String[] reverseArr = new String[array.length];
  67. reverseArr = reverse(array);
  68. if (Arrays.equals(reverseArr, array)){
  69. output = true;
  70. }
  71. return output;
  72. }
  73. /**
  74. * @param array array of String objects
  75. * @return true if each letter in the alphabet has been used in the array
  76. */ // TODO
  77. public static boolean isPangramic(String[] array) {
  78. String[] arr = makeSplitStringArray(array);
  79. boolean isPang = containsLetter(arr);
  80. return isPang;
  81. }
  82. public static boolean containsLetter(String[] array){
  83. boolean doesContain = true;
  84. String[] alph = new String[]{"a", "b", "c", "d", "e", "f", "g", "h",
  85. "i", "j", "k", "l", "m", "n", "o", "p",
  86. "q", "r", "s", "t", "u", "v", "w", "x",
  87. "y", "z"};
  88. for (String check: alph){
  89. doesContain = contains(array, check);
  90. if(doesContain == false){
  91. break;
  92. }
  93. }
  94. return doesContain;
  95. }
  96. public static String[] makeSplitStringArray(String[] array){
  97. StringBuilder fullString = new StringBuilder();
  98. for(String s : array) {
  99. fullString.append(s);
  100. }
  101. String str = fullString.toString().toLowerCase();
  102. String[] individualLetters = str.split("");
  103. return individualLetters;
  104. }
  105. /**
  106. * @param array array of String objects
  107. * @param value value to check array for
  108. * @return number of occurrences the specified `value` has occurred
  109. */ // TODO
  110. public static int getNumberOfOccurrences(String[] array, String value) {
  111. int numOfOcc = 0;
  112. for (String element: array){
  113. if (element.equals(value)){
  114. numOfOcc++;
  115. }
  116. }
  117. return numOfOcc;
  118. }
  119. /**
  120. * @param array array of String objects
  121. * @param valueToRemove value to remove from array
  122. * @return array with identical contents excluding values of `value`
  123. */ // TODO
  124. public static String[] removeValue(String[] array, String valueToRemove) {
  125. int howManyToDelete = getNumberOfOccurrences(array, valueToRemove);
  126. String[] revisedArray = new String[array.length-howManyToDelete];
  127. int index = 0;
  128. for(String element: array){
  129. if(element != valueToRemove){
  130. revisedArray[index] = element;
  131. index++;
  132. }
  133. }
  134. return revisedArray;
  135. }
  136. public static String[] sortArray(String[] array){
  137. String[] sorted = new String[array.length];
  138. Arrays.sort(sorted);
  139. return sorted;
  140. }
  141. /**
  142. * @param array array of chars
  143. * @return array of Strings with consecutive duplicates removes
  144. */ // TODO
  145. public static String[] removeConsecutiveDuplicates(String[] array) {
  146. int dupes = countDuplicates(array);
  147. String[] revisedArr = new String[array.length-dupes];
  148. int j = 0;
  149. for(int i = 0; i < array.length-1; i++){
  150. if (array[i]!=(array[i+1])){
  151. revisedArr[j] = array[i];
  152. j++;
  153. }
  154. }
  155. revisedArr[revisedArr.length-1]=array[array.length-1];
  156. return revisedArr;
  157. }
  158. public static int countDuplicates(String[] array){
  159. String[] newArray = array;
  160. int counter = 0;
  161. for(int i = 0; i < newArray.length-1; i++){
  162. if (newArray[i].equals(newArray[i+1])){
  163. counter++;
  164. }
  165. }
  166. return counter;
  167. }
  168. /**
  169. * @param array array of chars
  170. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  171. */ // TODO
  172. public static String[] packConsecutiveDuplicates(String[] array) {
  173. //int dupes = countDuplicates(array);
  174. //String[] revisedArr = new String[array.length-dupes];
  175. StringBuilder full = new StringBuilder();
  176. full.append(array[0]);
  177. //revisedArr[0]=array[0];
  178. StringBuilder temp = new StringBuilder();
  179. for(int i = 1; i < array.length; i++) {
  180. if(array[i].equals(array[i-1])) {
  181. full.append(array[i]);
  182. //temp.append(array[i]);
  183. } else {
  184. full.append(" ");
  185. full.append(array[i]);
  186. //revisedArr[i]=temp.toString();
  187. //temp.setLength(0);
  188. }
  189. }
  190. //String[] cleanPack = full.toString();
  191. String[] cleanPack = full.toString().split(" ");
  192. return cleanPack;
  193. //return revisedArr;
  194. }
  195. }