StringArrayUtils.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import java.util.*;
  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 result = false;
  41. for (String val : array) {
  42. if (val.equals(value)) result = true;
  43. }
  44. return result;
  45. }
  46. /**
  47. * @param array of String objects
  48. * @return an array with identical contents in reverse order
  49. */ // TODO
  50. public static String[] reverse(String[] array) {
  51. String[] reverseArray = new String[array.length];
  52. int j = 0;
  53. for (int i = array.length - 1; i >= 0; i--) {
  54. reverseArray[j] = array[i];
  55. j++;
  56. }
  57. return reverseArray;
  58. }
  59. /**
  60. * @param array array of String objects
  61. * @return true if the order of the array is the same backwards and forwards
  62. */ // TODO
  63. public static boolean isPalindromic(String[] array) {
  64. boolean result = true;
  65. String[] revArr = new String[array.length];
  66. int j = 0;
  67. for (int i = array.length - 1; i >= 0; i--) {
  68. revArr[j] = array[i];
  69. j++;
  70. }
  71. for (int i = 0; i < array.length - 1; i++) {
  72. if (!(array[i].equals(revArr[i]))) result = false;
  73. }
  74. return result;
  75. }
  76. /**
  77. * @param array array of String objects
  78. * @return true if each letter in the alphabet has been used in the array
  79. */ // TODO
  80. public static boolean isPangramic(String[] array) {
  81. String alpha = "abcdefghijklmnopqrstuvwxyz";
  82. for (int i = 0; i < array.length; i++) {
  83. String word = array[i];
  84. for (int j = 0; j < word.length(); j++) {
  85. String letter = Character.toString(word.charAt(j)).toLowerCase();
  86. if (alpha.indexOf(letter) != -1) {
  87. alpha = alpha.replace(letter, "");
  88. }
  89. }
  90. }
  91. boolean result = alpha.length() <= 0;
  92. return result;
  93. }
  94. /**
  95. * @param array array of String objects
  96. * @param value value to check array for
  97. * @return number of occurrences the specified `value` has occurred
  98. */ // TODO
  99. public static int getNumberOfOccurrences(String[] array, String value) {
  100. int result = 0;
  101. for (String val : array) {
  102. if (val.equals(value)) result += 1;
  103. }
  104. return result;
  105. }
  106. /**
  107. * @param array array of String objects
  108. * @param valueToRemove value to remove from array
  109. * @return array with identical contents excluding values of `value`
  110. */ // TODO
  111. public static String[] removeValue(String[] array, String valueToRemove) {
  112. int count = 0;
  113. for (int i = 0; i < array.length; i++) {
  114. if(array[i].equals(valueToRemove)) count++;
  115. }
  116. String[] newArr = new String[array.length - count];
  117. int j = 0;
  118. for (int i = 0; i < array.length; i++) {
  119. if (!array[i].equals(valueToRemove)) {
  120. newArr[j] = array[i];
  121. j++;
  122. }
  123. }
  124. return newArr;
  125. }
  126. /**
  127. * @param array array of chars
  128. * @return array of Strings with consecutive duplicates removes
  129. */ // TODO
  130. public static String[] removeConsecutiveDuplicates(String[] array) {
  131. int count = 0;
  132. for (int i = 0; i < array.length - 1; i++) {
  133. if (array[i].equals(array[i + 1])) count++;
  134. }
  135. String[] newArr = new String[array.length - count];
  136. newArr[newArr.length - 1] = array[array.length - 1];
  137. int j = 0;
  138. for (int i = 0; i < array.length - 1; i++) {
  139. if (!array[i].equals(array[i + 1])) {
  140. newArr[j] = array[i];
  141. j++;
  142. }
  143. }
  144. return newArr;
  145. }
  146. /**
  147. * @param array array of chars
  148. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  149. */ // TODO
  150. public static String[] packConsecutiveDuplicates(String[] array) {
  151. int count = 1;
  152. for (int i = 0; i < array.length - 1; i++) {
  153. System.out.println(!array[i].equals(array[i + 1]));
  154. if (!array[i].equals(array[i + 1])) count++;
  155. }
  156. String[] newArr = new String[count];
  157. StringBuilder temp = new StringBuilder();
  158. for (int i = array.length - 1; i >= 0; i--) {
  159. if (array[i].equals(array[i - 1])) {
  160. temp.append(array[i]);
  161. } else {
  162. temp.append(array[i]);
  163. newArr[newArr.length - 1] = temp.toString();
  164. temp = new StringBuilder();
  165. break;
  166. }
  167. }
  168. int j = 0;
  169. for (int i = 0; i < array.length - 1; i++) {
  170. if (array[i].equals(array[i + 1])) {
  171. temp.append(array[i]);
  172. } else {
  173. temp.append(array[i]);
  174. newArr[j] = temp.toString();
  175. temp = new StringBuilder();
  176. j++;
  177. }
  178. }
  179. System.out.println(Arrays.toString(newArr));
  180. return newArr;
  181. }
  182. }