StringArrayUtils.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. String result = "";
  12. result += array[0];
  13. return result ;
  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. String result = "";
  21. result += array[1];
  22. return result ;
  23. }
  24. /**
  25. * @param array array of String objects
  26. * @return last element in specified array
  27. */ // TODO
  28. public static String getLastElement(String[] array) {
  29. String result = "";
  30. return array[array.length-1] ;
  31. }
  32. /**
  33. * @param array array of String objects
  34. * @return second to last element in specified array
  35. */ // TODO
  36. public static String getSecondToLastElement(String[] array) {
  37. String result = "";
  38. return array[array.length-2];
  39. }
  40. /**
  41. * @param array array of String objects
  42. * @param value value to check array for
  43. * @return true if the array contains the specified `value`
  44. */ // TODO
  45. public static boolean contains(String[] array, String value) {
  46. boolean x = false;
  47. for (String i : array){
  48. if (i == value){
  49. x = true;
  50. }
  51. }
  52. return x;
  53. }
  54. /**
  55. * @param array of String objects
  56. * @return an array with identical contents in reverse order
  57. */ // TODO
  58. public static String[] reverse(String[] array) {
  59. String order;
  60. for (int x = 0; x < array.length/2; x++){
  61. order = array[x];
  62. array[x] = array[array.length -x -1];
  63. array[array.length -x -1] = order;
  64. }
  65. return array;
  66. }
  67. /**
  68. * @param array array of String objects
  69. * @return true if the order of the array is the same backwards and forwards
  70. */ // TODO
  71. public static boolean isPalindromic(String[] array) {
  72. boolean rev = false;
  73. for (int x = 0; x < array.length/2; x++){
  74. if (array[x] == array[array.length -x -1]){
  75. rev = true;
  76. }
  77. }
  78. return rev;
  79. }
  80. /**
  81. * @param array array of String objects
  82. * @return true if each letter in the alphabet has been used in the array
  83. */ // TODO
  84. public static boolean isPangramic(String[] array) {
  85. boolean every = false;
  86. StringBuilder build = new StringBuilder();
  87. for(String s: array){
  88. build.append(s);
  89. }
  90. String ofArr = "";
  91. ofArr = build.toString();
  92. for (char x = 'A'; x < 'Z'; x++){
  93. if ((ofArr.indexOf(x) <0) && (ofArr.indexOf((char) (x + 32)) <0)){
  94. every =false;
  95. }
  96. else{ every = true;
  97. }
  98. }
  99. return every;
  100. }
  101. /**
  102. * @param array array of String objects
  103. * @param value value to check array for
  104. * @return number of occurrences the specified `value` has occurred
  105. */ // TODO
  106. public static int getNumberOfOccurrences(String[] array, String value) {
  107. int occur = 0;
  108. for (int x = 0; x < array.length; x++){
  109. if (value.equals(array[x])){
  110. occur++;
  111. }
  112. }
  113. return occur;
  114. }
  115. /**
  116. * @param array array of String objects
  117. * @param valueToRemove value to remove from array
  118. * @return array with identical contents excluding values of `value`
  119. */ // TODO
  120. public static String[] removeValue(String[] array, String valueToRemove) {
  121. int place = 0;
  122. int count = getNumberOfOccurrences(array , valueToRemove);
  123. String[] newArr = new String [array.length - count];
  124. for (int x = 0; x < array.length; x++){
  125. if (array[x] != valueToRemove){
  126. newArr[place] = array[x];
  127. place++;
  128. }
  129. }
  130. return newArr;
  131. }
  132. /**
  133. * @param array array of chars
  134. * @return array of Strings with consecutive duplicates removes
  135. */ // TODO
  136. public static String[] removeConsecutiveDuplicates(String[] array) {
  137. int newArrIndex= 0;
  138. String[] newArr = new String[array.length];
  139. newArr[newArrIndex] = array[0];
  140. newArrIndex++;
  141. for (int x = 1; x < array.length; x++){
  142. if (!array[x].equals(newArr[newArrIndex - 1])){
  143. newArr[newArrIndex]= array[x];
  144. newArrIndex++;
  145. }
  146. }
  147. String[] newArr2 = new String [newArrIndex];
  148. System.arraycopy(newArr, 0 , newArr2, 0, newArrIndex);
  149. return newArr2;
  150. }
  151. /**
  152. * @param array array of chars
  153. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  154. */ // TODO
  155. public static String[] packConsecutiveDuplicates(String[] array) {
  156. StringBuilder dups = new StringBuilder(array[0]);
  157. String[] newArr = new String[array.length-count(array)];
  158. int adding = 0;
  159. for (int x = 1; x < array.length; x++){
  160. if (array[x-1].equals(array[x])){
  161. dups.append(array[x]);
  162. }
  163. else {
  164. newArr[adding] = dups.toString();
  165. dups.setLength(0);
  166. dups.append(array[x]);
  167. adding++;
  168. }
  169. }
  170. newArr[adding] = dups.toString();
  171. return newArr;
  172. }
  173. public static int count(String[] array){
  174. int count = 0;
  175. for (int x = 1; x < array.length; x++){
  176. if (array[x-1].equals(array[x])){
  177. count++;
  178. }
  179. }
  180. return count;
  181. }
  182. }