StringArrayUtils.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 element : array) {
  42. if (element.equals(value)) {
  43. output = true;
  44. }
  45. }
  46. return output;
  47. }
  48. /**
  49. * @param array of String objects
  50. * @return an array with identical contents in reverse order
  51. */ // TODO
  52. public static String[] reverse(String[] array) {
  53. String[] reversedArray = new String[array.length];
  54. for (int i=array.length-1; i >= 0; i--) {
  55. reversedArray[array.length-1-i] = array[i];
  56. }
  57. return reversedArray;
  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 output = false;
  65. String[] reversedArray = reverse(array);
  66. if (Arrays.equals(array, reversedArray)) {
  67. output = true;
  68. }
  69. return output;
  70. }
  71. /**
  72. * @param array array of String objects
  73. * @return true if each letter in the alphabet has been used in the array
  74. */ // TODO
  75. public static boolean isPangramic(String[] array) {
  76. boolean outcome = true;
  77. String[] arrayGiven = new String[1];
  78. arrayGiven[0]="";
  79. for (int i=0; i < array.length; i++) {
  80. arrayGiven[0] = arrayGiven[0].concat(array[i]);
  81. }
  82. arrayGiven = arrayGiven[0].toLowerCase().split("");
  83. for (char alph = 'a'; alph <= 'z'; alph++) {
  84. if (contains(arrayGiven, String.valueOf(alph))==false) {
  85. outcome = false;
  86. }
  87. }
  88. return outcome;
  89. }
  90. /**
  91. * @param array array of String objects
  92. * @param value value to check array for
  93. * @return number of occurrences the specified `value` has occurred
  94. */ // TODO
  95. public static int getNumberOfOccurrences(String[] array, String value) {
  96. int counter = 0;
  97. for (String element : array) {
  98. if (element.equals(value)) {
  99. counter++;
  100. }
  101. }
  102. return counter;
  103. }
  104. /**
  105. * @param array array of String objects
  106. * @param valueToRemove value to remove from array
  107. * @return array with identical contents excluding values of `value`
  108. */ // TODO
  109. public static String[] removeValue(String[] array, String valueToRemove) {
  110. int numberOfElementsToDelete = getNumberOfOccurrences(array, valueToRemove);
  111. String[] arrayWithRemovedValue = new String[array.length-numberOfElementsToDelete];
  112. int j=0; //index of arrayWithRemovedValue
  113. for (int i=0; i < array.length; i++) {
  114. if (array[i].compareTo(valueToRemove) != 0) {
  115. arrayWithRemovedValue[j] = array[i];
  116. j++;
  117. }
  118. }
  119. return arrayWithRemovedValue;
  120. }
  121. /**
  122. * @param array array of chars
  123. * @return array of Strings with consecutive duplicates removes
  124. */ // TODO
  125. public static String[] removeConsecutiveDuplicates(String[] array) {
  126. int numberOfElementsToDelete = 0;
  127. int j=0; //index of arrayWithRemovedVConsecutiveDuplicates
  128. String[] arrayWithRemovedConsecutiveDuplicates = new String[array.length];
  129. for (int i=0; i < array.length-1; i++) {
  130. if (array[i].compareTo(array[i+1]) != 0) {
  131. arrayWithRemovedConsecutiveDuplicates[j] = array[i];
  132. j++;
  133. } else {
  134. numberOfElementsToDelete++;
  135. }
  136. }
  137. arrayWithRemovedConsecutiveDuplicates = Arrays.copyOf(arrayWithRemovedConsecutiveDuplicates, array.length-numberOfElementsToDelete);
  138. arrayWithRemovedConsecutiveDuplicates[arrayWithRemovedConsecutiveDuplicates.length-1] = array[array.length-1];
  139. return arrayWithRemovedConsecutiveDuplicates;
  140. }
  141. /**
  142. * @param array array of chars
  143. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  144. */ // TODO
  145. public static String[] packConsecutiveDuplicates(String[] array) {
  146. String[] arrayWithPackedConsecutiveDuplicates = new String[array.length];
  147. int j=0; //index of arrayWithPackedVConsecutiveDuplicates
  148. int numberOfElementsToDelete = 0;
  149. arrayWithPackedConsecutiveDuplicates[0] = array[0];
  150. for (int i=0; i < array.length-1; i++) {
  151. if (array[i].compareTo(array[i+1]) == 0) {
  152. arrayWithPackedConsecutiveDuplicates[j] = arrayWithPackedConsecutiveDuplicates[j] + array[i+1];
  153. numberOfElementsToDelete++;
  154. } else {
  155. arrayWithPackedConsecutiveDuplicates[j+1] = array[i+1];
  156. j++;
  157. }
  158. }
  159. arrayWithPackedConsecutiveDuplicates = Arrays.copyOf(arrayWithPackedConsecutiveDuplicates, array.length-numberOfElementsToDelete);
  160. return arrayWithPackedConsecutiveDuplicates;
  161. }
  162. }