StringArrayUtils.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * Created by leon on 1/29/18.
  3. */
  4. import java.util.Arrays;
  5. import java.lang.*;
  6. public class StringArrayUtils {
  7. /**
  8. * @param array array of String objects
  9. * @return first element of specified array
  10. */ // TODO
  11. static int countRoy = 0;
  12. public static String getFirstElement(String[] array) {
  13. return array[0];
  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. return array[1];
  21. }
  22. /**
  23. * @param array array of String objects
  24. * @return last element in specified array
  25. */ // TODO
  26. public static String getLastElement(String[] array) {
  27. return array[array.length-1];
  28. }
  29. /**
  30. * @param array array of String objects
  31. * @return second to last element in specified array
  32. */ // TODO
  33. public static String getSecondToLastElement(String[] array) {
  34. return array[array.length-2];
  35. }
  36. /**
  37. * @param array array of String objects
  38. * @param value value to check array for
  39. * @return true if the array contains the specified `value`
  40. */ // TODO
  41. public static boolean contains(String[] array, String value) {
  42. for(int i =0; i<array.length; i++){
  43. if (array[i].equals(value)){
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49. /**
  50. * @param array of String objects
  51. * @return an array with identical contents in reverse order
  52. */ // TODO
  53. public static String[] reverse(String[] array) {
  54. for(int i = 0 ; i < array.length/2; i++){
  55. String temp1 = array[i];
  56. array[i] = array[array.length-i-1];
  57. array[array.length-i-1] = temp1;
  58. }
  59. return array;
  60. }
  61. /**
  62. * @param array array of String objects
  63. * @return true if the order of the array is the same backwards and forwards
  64. */ // TODO
  65. public static boolean isPalindromic(String[] array) {
  66. // String[] arrayCopy = reverse(array);
  67. String[] arrayCopy = new String[array.length];
  68. int j = 0;
  69. for (int i = array.length - 1; i >= 0; i--) {
  70. System.out.println(j);
  71. arrayCopy[j++] = array[i];
  72. System.out.println(j);
  73. }
  74. if (Arrays.equals(array, arrayCopy)){
  75. return true;
  76. }
  77. //System.out.println(array[]);
  78. return false;
  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. String alphabet = "abcdefghijklmnopqrstuvwxyz";
  86. //System.out.println(newArray);
  87. boolean pangramic = true;
  88. for(int i =0; i<alphabet.length(); i++){
  89. boolean letterFound = false;
  90. for(int j =0; j<array.length; j++){
  91. if(array[j].toLowerCase().contains(Character.toString(alphabet.charAt(i)))){
  92. letterFound = true;
  93. break;
  94. }
  95. }
  96. if (!letterFound){
  97. pangramic = false;
  98. break;
  99. }
  100. }
  101. return pangramic;
  102. }
  103. /**
  104. * @param array array of String objects
  105. * @param value value to check array for
  106. * @return number of occurrences the specified `value` has occurred
  107. */ // TODO
  108. public static int getNumberOfOccurrences(String[] array, String value) {
  109. int numberOfOccurances =0;
  110. for(int i=0; i<array.length; i++){
  111. if (array[i] == value){
  112. numberOfOccurances++;
  113. }
  114. }
  115. return numberOfOccurances;
  116. }
  117. /**
  118. * @param array array of String objects
  119. * @param valueToRemove value to remove from array
  120. * @return array with identical contents excluding values of `value`
  121. */ // TODO
  122. public static String[] removeValue(String[] array, String valueToRemove) {
  123. int occurance = getNumberOfOccurrences(array, valueToRemove);
  124. String[] newArray = new String[array.length - occurance];
  125. int j = 0;
  126. for(int i=0; i<array.length; i++){
  127. if (array[i] != valueToRemove){
  128. newArray[j] = array[i];
  129. j++;
  130. }
  131. }
  132. return newArray;
  133. }
  134. public static int countOcuurances(String[] array){
  135. int count =0;
  136. for(int i=1; i<array.length; i++){
  137. if (array[i-1] == array[i]){
  138. count++;
  139. }
  140. }
  141. return count;
  142. }
  143. /**
  144. * @param array array of chars
  145. * @return array of Strings with consecutive duplicates removes
  146. */ // TODO
  147. public static String[] removeConsecutiveDuplicates(String[] array) {
  148. int count = countOcuurances(array);
  149. String[] newArray = new String[array.length - count];
  150. String[] expected = {"aba", "baa", "bab", "bba", "zzz", "bba", "aba", "bbb"};
  151. int j =0;
  152. for(int i=1; i<array.length; i++){
  153. if(array[i-1] != array[i] ){
  154. System.out.println(array[i]);
  155. newArray[j] = array[i-1];
  156. j++;
  157. }
  158. if (i == array.length - 1 ) {
  159. newArray[j] = array[array.length - 1];
  160. }
  161. }
  162. //System.out.println(newArray[0]);
  163. return newArray;
  164. }
  165. /**
  166. * @param array array of chars
  167. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  168. */ // TODO
  169. public static String[] packConsecutiveDuplicates(String[] array) {
  170. int count = countOcuurances(array);
  171. int j=0;
  172. String[] newArray = new String[array.length - count];
  173. newArray[0] = array[0];
  174. for(int i=1; i<array.length; i++){
  175. if(array[i-1].equals(array[i]) ){
  176. //System.out.println(array[i]);
  177. newArray[j] += array[i];
  178. //j++;
  179. }
  180. else {
  181. j++;
  182. newArray[j] = array[i];
  183. }
  184. // else if (i == array.length - 1 ) {
  185. // newArray[j] = array[array.length - 1];
  186. // }
  187. }
  188. return newArray;
  189. }
  190. }