StringArrayUtils.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import java.util.Arrays;
  2. public class StringArrayUtils {
  3. /**
  4. * @param array array of String objects
  5. * @return first element of specified array
  6. */ // TODO
  7. public static String getFirstElement(String[] array) {
  8. return array[0];
  9. }
  10. /**
  11. * @param array array of String objects
  12. * @return second element in specified array
  13. */
  14. public static String getSecondElement(String[] array) {
  15. return array[1];
  16. }
  17. /**
  18. * @param array array of String objects
  19. * @return last element in specified array
  20. */ // TODO
  21. public static String getLastElement(String[] array) {
  22. return array[array.length-1];
  23. }
  24. /**
  25. * @param array array of String objects
  26. * @return second to last element in specified array
  27. */ // TODO
  28. public static String getSecondToLastElement(String[] array) {
  29. return array[array.length-2];
  30. }
  31. /**
  32. * @param array array of String objects
  33. * @param value value to check array for
  34. * @return true if the array contains the specified `value`
  35. */ // TODO
  36. public static boolean contains(String[] array, String value) {
  37. boolean answer = false;
  38. for (String x: array){
  39. if (x.equals(value))
  40. answer = true;
  41. }
  42. return answer;
  43. }
  44. /**
  45. * @param array of String objects
  46. * @return an array with identical contents in reverse order
  47. */ // TODO
  48. public static String[] reverse(String[] array) {
  49. String [] str = new String [array.length];
  50. int count = 0;
  51. for (int i = array.length-1; i>=0; i--){
  52. str[count] = array[i];
  53. count=count+1;
  54. }
  55. return str;
  56. }
  57. /**
  58. * @param array array of String objects
  59. * @return true if the order of the array is the same backwards and forwards
  60. */ // TODO
  61. public static boolean isPalindromic(String[] array) {
  62. String [] newArray = reverse(array);
  63. if (Arrays.equals(newArray, array))
  64. return true;
  65. else
  66. return false;
  67. }
  68. /**
  69. * @param array array of String objects
  70. * @return true if each letter in the alphabet has been used in the array
  71. */ // TODO
  72. public static boolean isPangramic(String[] array) {
  73. boolean flag = false;
  74. String [] alphabet = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
  75. StringBuilder sb = new StringBuilder();
  76. for (String element: array)
  77. sb.append(element);
  78. String str = sb.toString();
  79. for (String letter : alphabet){
  80. if (str.contains(letter))
  81. flag = true;
  82. else
  83. flag = false;
  84. }
  85. return flag;
  86. }
  87. /**
  88. * @param array array of String objects
  89. * @param value value to check array for
  90. * @return number of occurrences the specified `value` has occurred
  91. */ // TODO
  92. public static int getNumberOfOccurrences(String[] array, String value) {
  93. int numberOfOccurences = 0;
  94. for (int i = 0; i <=array.length-1; i++){
  95. if (array[i]==value)
  96. numberOfOccurences+=1;
  97. }
  98. return numberOfOccurences;
  99. }
  100. /**
  101. * @param array array of String objects
  102. * @param valueToRemove value to remove from array
  103. * @return array with identical contents excluding values of `value`
  104. */ // TODO
  105. public static String[] removeValue(String[] array, String valueToRemove) {
  106. String [] strWithoutValue = new String [array.length-1];
  107. int count = 0;
  108. for (int i = 0; i < array.length; i++){
  109. if (!array[i].equals(valueToRemove)){
  110. strWithoutValue[count]=array[i];
  111. count += 1;
  112. }
  113. }
  114. return strWithoutValue;
  115. }
  116. /**
  117. * @param array array of chars
  118. * @return array of Strings with consecutive duplicates removes
  119. */ // TODO
  120. public static String[] removeConsecutiveDuplicates(String[] array) {
  121. String [] newArray = new String[array.length];
  122. int positionOfNewArray = 0;
  123. for (int i = 0; i <= array.length-2; i++){
  124. if (!array[i].equals(array[i+1])){
  125. newArray[positionOfNewArray] = array[i];
  126. positionOfNewArray+=1;
  127. }
  128. }
  129. newArray[positionOfNewArray] = array[array.length-1];
  130. String [] finalArray = new String [positionOfNewArray+1];
  131. for (int i = 0; i< positionOfNewArray+1; i++){
  132. finalArray[i] = newArray[i];
  133. }
  134. return finalArray;
  135. }
  136. /**
  137. * @param array array of chars
  138. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  139. */ // TODO
  140. public static String[] packConsecutiveDuplicates(String[] array) {
  141. StringBuilder sb = new StringBuilder();
  142. for (int i = 0; i < array.length-1; i++){
  143. if (array[i].equals(array[i+1]))
  144. sb.append(array[i]);
  145. else {
  146. sb.append(array[i]);
  147. sb.append(" ");
  148. }
  149. }
  150. sb.append(array[array.length-1]);
  151. String str = sb.toString();
  152. String [] newArray = str.split(" ");
  153. return newArray;
  154. }
  155. }