StringArrayUtils.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. public void println(){
  104. String[] s = {"Is this a plaindrome?", "This is not a plaindrome", "Is this a palindrome?", "This is not a palindrome"};
  105. s = reverse(s);
  106. for (String sa: s){
  107. System.out.println(sa);
  108. }
  109. System.out.println(countRoy);
  110. }
  111. /**
  112. * @param array array of String objects
  113. * @param value value to check array for
  114. * @return number of occurrences the specified `value` has occurred
  115. */ // TODO
  116. public static int getNumberOfOccurrences(String[] array, String value) {
  117. int numberOfOccurances =0;
  118. for(int i=0; i<array.length; i++){
  119. if (array[i] == value){
  120. numberOfOccurances++;
  121. }
  122. }
  123. return numberOfOccurances;
  124. }
  125. /**
  126. * @param array array of String objects
  127. * @param valueToRemove value to remove from array
  128. * @return array with identical contents excluding values of `value`
  129. */ // TODO
  130. public static String[] removeValue(String[] array, String valueToRemove) {
  131. int occurance = getNumberOfOccurrences(array, valueToRemove);
  132. String[] newArray = new String[array.length - occurance];
  133. int j = 0;
  134. for(int i=0; i<array.length; i++){
  135. if (array[i] != valueToRemove){
  136. newArray[j] = array[i];
  137. j++;
  138. }
  139. }
  140. return newArray;
  141. }
  142. public static int countOcuurances(String[] array){
  143. int count =0;
  144. for(int i=1; i<array.length; i++){
  145. if (array[i-1] == array[i]){
  146. count++;
  147. }
  148. }
  149. return count;
  150. }
  151. /**
  152. * @param array array of chars
  153. * @return array of Strings with consecutive duplicates removes
  154. */ // TODO
  155. public static String[] removeConsecutiveDuplicates(String[] array) {
  156. int count = countOcuurances(array);
  157. String[] newArray = new String[array.length - count];
  158. String[] expected = {"aba", "baa", "bab", "bba", "zzz", "bba", "aba", "bbb"};
  159. int j =0;
  160. for(int i=1; i<array.length; i++){
  161. if(array[i-1] != array[i] ){
  162. System.out.println(array[i]);
  163. newArray[j] = array[i-1];
  164. j++;
  165. }
  166. if (i == array.length - 1 ) {
  167. newArray[j] = array[array.length - 1];
  168. }
  169. }
  170. //System.out.println(newArray[0]);
  171. return newArray;
  172. }
  173. /**
  174. * @param array array of chars
  175. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  176. */ // TODO
  177. public static String[] packConsecutiveDuplicates(String[] array) {
  178. int count = countOcuurances(array);
  179. int j=0;
  180. String[] newArray = new String[array.length - count];
  181. newArray[0] = array[0];
  182. for(int i=1; i<array.length; i++){
  183. if(array[i-1].equals(array[i]) ){
  184. //System.out.println(array[i]);
  185. newArray[j] += array[i];
  186. //j++;
  187. }
  188. else {
  189. j++;
  190. newArray[j] = array[i];
  191. }
  192. // else if (i == array.length - 1 ) {
  193. // newArray[j] = array[array.length - 1];
  194. // }
  195. }
  196. return newArray;
  197. }
  198. }