123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * Created by leon on 1/29/18.
  3. */
  4. //import Arrays.array
  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];//null;
  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 (int i =0; i < array.length; i++) {
  41. if (array[i].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[] arr = new String[array.length];
  53. int n = 0;
  54. for (int i = array.length -1; i >=0; i--){
  55. arr[n] = array[i];
  56. n++;
  57. }
  58. return arr;
  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 bool = false;
  66. for (int i = 0; i < array.length / 2; i++){
  67. if (array[i] == array[array.length-i-1]){
  68. bool = true;
  69. }
  70. }
  71. return bool;
  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. boolean bool = false;
  79. StringBuilder stringBuilder = new StringBuilder();
  80. for (String x : array){
  81. stringBuilder.append(x);
  82. }
  83. String s = "";
  84. s = stringBuilder.toString();
  85. for(char i = 'A'; i <= 'Z'; i++){
  86. if ((s.indexOf(i) < 0) && (s.indexOf((char)i+32)< 0)){
  87. bool = false;
  88. }else{
  89. bool = true;
  90. }
  91. }
  92. return bool;
  93. }
  94. /**
  95. * @param array array of String objects
  96. * @param value value to check array for
  97. * @return number of occurrences the specified `value` has occurred
  98. */ // TODO
  99. public static int getNumberOfOccurrences(String[] array, String value) {
  100. int count = 0;
  101. for (int i = 0; i < array.length; i++){
  102. if(array[i].equals(value)) {
  103. count++;
  104. }
  105. }
  106. return count;
  107. }
  108. /**
  109. * @param array array of String objects
  110. * @param valueToRemove value to remove from array
  111. * @return array with identical contents excluding values of `value`
  112. */ // TODO
  113. public static String[] removeValue(String[] array, String valueToRemove) {
  114. int count = 0;
  115. int occurrences = getNumberOfOccurrences(array, valueToRemove);
  116. String[] arr = new String[array.length-occurrences];
  117. for (String s : array) {
  118. if (s != valueToRemove) {
  119. arr[count] = s;
  120. count++;
  121. }
  122. }
  123. return arr;
  124. }
  125. /**
  126. * @param array array of chars
  127. * @return array of Strings with consecutive duplicates removes
  128. */ // TODO
  129. public static String[] removeConsecutiveDuplicates(String[] array) {
  130. String [] arr = new String[array.length];
  131. int newArrayIndex = 0;
  132. arr[newArrayIndex] = array[0];
  133. newArrayIndex++;
  134. for (int i = 1; i < array.length; i++){
  135. if(!(arr[newArrayIndex-1]).equals(array[i])){
  136. arr[newArrayIndex] = array[i];
  137. newArrayIndex++;
  138. }
  139. }
  140. String[] newArray = new String[newArrayIndex];
  141. System.arraycopy(arr, 0, newArray, 0, newArrayIndex);
  142. return newArray;
  143. }
  144. /**
  145. * @param array array of chars
  146. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  147. */ // TODO
  148. public static String[] packConsecutiveDuplicates(String[] array) {
  149. StringBuilder build = new StringBuilder(array[0]);
  150. String[] newArr = new String[array.length-count(array)];
  151. int counter = 0;
  152. for(int i = 1; i<array.length; i++){
  153. if(array[i-1].equals(array[i])){
  154. build.append(array[i]);
  155. }
  156. else{
  157. newArr[counter] = build.toString();
  158. build.setLength(0);
  159. build.append(array[i]);
  160. counter++;
  161. }
  162. }
  163. newArr[counter] = build.toString();
  164. return newArr;
  165. }
  166. public static int count(String[] array){
  167. int count = 0;
  168. for(int i = 1; i<array.length; i++){
  169. if(array[i-1].equals(array[i])) {
  170. count++;
  171. }
  172. }
  173. return count;
  174. }
  175. }