StringArrayUtils.java 5.9KB

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