StringArrayUtils.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. for(String answer : array){
  41. if(answer.equals(value)){
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. /**
  48. * @param array of String objects
  49. * @return an array with identical contents in reverse order
  50. */ // TODO
  51. public static String[] reverse(String[] array) {
  52. String[] output = new String[array.length];
  53. int counter = 0;
  54. for (int i = array.length -1; i >= 0; i--) {
  55. output[counter] = array[i];
  56. counter++;
  57. }
  58. return output;
  59. }
  60. /**
  61. * @param array array of String objects
  62. * @return true if the order of the array is the same backwards and forwards
  63. */ // TODO
  64. public static boolean isPalindromic(String[] array) {
  65. boolean palindromic = false;
  66. for (int i = 0; i <= array.length/2; i++) {
  67. if (array[i] == array[array.length -1 -i]) {
  68. palindromic = true;
  69. }
  70. }
  71. return palindromic;
  72. }
  73. /**
  74. * @param array array of String objects
  75. * @return true if each letter in the alphabet has been used in the array
  76. */ // TODO
  77. public static boolean isPangramic(String[] array) {
  78. String[] alphabet = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
  79. "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
  80. "w", "x", "y", "z" };
  81. boolean pan = true;
  82. for (String letter : alphabet) {
  83. boolean letterFound = false;
  84. for (String letter2 : array) {
  85. if (letter2.toLowerCase().contains(letter)) {
  86. letterFound = true;
  87. }
  88. }
  89. if (!letterFound) {
  90. pan = false;
  91. }
  92. }
  93. return pan;
  94. }
  95. /**
  96. * @param array array of String objects
  97. * @param value value to check array for
  98. * @return number of occurrences the specified `value` has occurred
  99. */ // TODO
  100. public static int getNumberOfOccurrences(String[] array, String value) {
  101. int counter = 0;
  102. for(String occurence : array){
  103. if(occurence.equals(value)) counter++;
  104. }
  105. return counter;
  106. }
  107. /**
  108. * @param array array of String objects
  109. * @param valueToRemove value to remove from array
  110. * @return array with identical contents excluding values of `value`
  111. */ // TODO
  112. public static String[] removeValue(String[] array, String valueToRemove) {
  113. int occurrences = getNumberOfOccurrences(array, valueToRemove);
  114. String[] valueRemoved;
  115. if(occurrences == 0){
  116. valueRemoved = array;
  117. } else {
  118. valueRemoved = new String[array.length - occurrences];
  119. int removedArr = 0;
  120. for (int i = 0; i < array.length; i++) {
  121. if (!array[i].equals(valueToRemove)) {
  122. valueRemoved[removedArr] = array[i];
  123. removedArr++;
  124. }
  125. }
  126. }
  127. return valueRemoved;
  128. }
  129. /**
  130. * @param array array of chars
  131. * @return array of Strings with consecutive duplicates removes
  132. */ // TODO
  133. public static String[] removeConsecutiveDuplicates(String[] array) {
  134. String lastNumber = null;
  135. int index = 0;
  136. int counter = 0;
  137. for (int i = 0; i < array.length - 1; i++) {
  138. if (array[i].equals(array[i + 1])) {
  139. counter++;
  140. }
  141. }
  142. String [] arr1 = new String [array.length - counter];
  143. for (int i = 0; i < array.length; i++) {
  144. if (!array[i].equals(lastNumber)){
  145. arr1[index] = array[i];
  146. lastNumber = array[i];
  147. index++;
  148. }
  149. }
  150. return arr1;
  151. }
  152. /**
  153. * @param array array of chars
  154. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  155. */ // TODO
  156. public static String[] packConsecutiveDuplicates(String[] array) {
  157. String[] consecDups = new String[array.length];
  158. int j=0;
  159. int counter = 0;
  160. consecDups[0] = array[0];
  161. for (int i = 0; i < array.length-1; i++) {
  162. if (array[i].compareTo(array[i+1]) == 0) {
  163. consecDups[j] = consecDups[j] + array[i+1];
  164. counter++;
  165. } else {
  166. consecDups[j+1] = array[i+1];
  167. j++;
  168. }
  169. }
  170. consecDups = Arrays.copyOf(consecDups, array.length-counter);
  171. return consecDups;
  172. }
  173. }