StringArrayUtils.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. /**
  5. * Created by leon on 1/29/18.
  6. */
  7. public class StringArrayUtils {
  8. /**
  9. * @param array array of String objects
  10. * @return first element of specified array
  11. */ // TODO
  12. public static String getFirstElement(String[] array) {
  13. return array[0];
  14. }
  15. /**
  16. * @param array array of String objects
  17. * @return second element in specified array
  18. */
  19. public static String getSecondElement(String[] array) {
  20. return array[1];
  21. }
  22. /**
  23. * @param array array of String objects
  24. * @return last element in specified array
  25. */ // TODO
  26. public static String getLastElement(String[] array) {
  27. return array[array.length - 1];
  28. }
  29. /**
  30. * @param array array of String objects
  31. * @return second to last element in specified array
  32. */ // TODO
  33. public static String getSecondToLastElement(String[] array) {
  34. return array[array.length - 2];
  35. }
  36. /**
  37. * @param array array of String objects
  38. * @param value value to check array for
  39. * @return true if the array contains the specified `value`
  40. */ // TODO
  41. public static boolean contains(String[] array, String value) {
  42. boolean contains = false;
  43. for (String ithString : array) {
  44. if (ithString.equals(value)) {
  45. contains = true;
  46. break;
  47. }
  48. }
  49. return contains;
  50. }
  51. /**
  52. * @param array of String objects
  53. * @return an array with identical contents in reverse order
  54. */ // TODO
  55. public static String[] reverse(String[] array) {
  56. String[] reverse = new String[array.length];
  57. for (int i = 0; i < array.length; i++) {
  58. reverse[i] = array[array.length - 1 - i];
  59. }
  60. return reverse;
  61. }
  62. /**
  63. * @param array array of String objects
  64. * @return true if the order of the array is the same backwards and forwards
  65. */ // TODO
  66. public static boolean isPalindromic(String[] array) {
  67. String[] reverse = reverse(array);
  68. boolean isPalindrome = true;
  69. for (int i = 0; i < array.length; i++) {
  70. if (!array[i].equals(reverse[i])) {
  71. isPalindrome = false;
  72. break;
  73. }
  74. }
  75. return isPalindrome;
  76. }
  77. /**
  78. * @param array array of String objects
  79. * @return true if each letter in the alphabet has been used in the array
  80. */ // TODO
  81. public static boolean isPangramic(String[] array) {
  82. boolean isPangramic = true;
  83. String alpha = "abcdefghijklmnopqrstuvwxyz";
  84. String[] alphaArr = newArray(alpha);
  85. StringBuilder allChars = new StringBuilder();
  86. for(String word1 : array) {
  87. allChars.append(word1);
  88. }
  89. String[] allCharsArr = newArray(allChars.toString().toLowerCase());
  90. for (String letter1 : alphaArr) {
  91. if (!contains(allCharsArr, letter1)) {
  92. isPangramic = false;
  93. break;
  94. }
  95. }
  96. return isPangramic;
  97. }
  98. public static String[] newArray(String word) {
  99. String[] newArr = new String[word.length()];
  100. for (int i = 0; i < word.length(); i++) {
  101. newArr[i] = "" + word.charAt(i);
  102. }
  103. return newArr;
  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 occurrences = 0;
  112. for (String word : array) {
  113. if (value.equals(word)) {
  114. occurrences++;
  115. }
  116. }
  117. return occurrences;
  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 numOcc = getNumberOfOccurrences(array, valueToRemove);
  126. String[] removedArr = new String[array.length - numOcc];
  127. int i = 0;
  128. for (String word1 : array) {
  129. if (!word1.equals(valueToRemove)) {
  130. removedArr[i] = word1;
  131. i++;
  132. }
  133. }
  134. return removedArr;
  135. }
  136. public static int countDuplicated(String[] array) {
  137. int count = 0;
  138. for (int i = 1; i < array.length; i++) {
  139. if (array[i].equals(array[i - 1])) {
  140. count++;
  141. }
  142. }
  143. return count;
  144. }
  145. /**
  146. * @param array array of chars
  147. * @return array of Strings with consecutive duplicates removes
  148. */ // TODO
  149. public static String[] removeConsecutiveDuplicates(String[] array) {
  150. int totalDups = countDuplicated(array);
  151. String[] removedArr = new String[array.length - totalDups];
  152. int index = 1;
  153. removedArr[0] = array[0];
  154. for (int i = 1; i < array.length; i++) {
  155. if (!array[i].equals(array[i - 1])) {
  156. removedArr[index] = array[i];
  157. index++;
  158. }
  159. }
  160. return removedArr;
  161. }
  162. /**
  163. * @param array array of chars
  164. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  165. */ // TODO
  166. public static String[] packConsecutiveDuplicates(String[] array) {
  167. StringBuilder dupB = new StringBuilder(array[0]);
  168. String[] packedDupArr= new String[array.length - countDuplicated(array)];
  169. int index = 0;
  170. for (int i = 1; i < array.length; i++) {
  171. if (array[i].equals(array[i - 1])) {
  172. dupB.append(array[i]);
  173. } else {
  174. packedDupArr[index] = dupB.toString();
  175. dupB.setLength(0);
  176. dupB.append(array[i]);
  177. index++;
  178. }
  179. }
  180. packedDupArr[index] = dupB.toString();
  181. return packedDupArr;
  182. }
  183. }