StringArrayUtils.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. //String[] array = {"brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  26. String word = "";
  27. for(int i = 0; i < array.length -1; i++){
  28. return word = array[array.length-1];
  29. }
  30. return word;
  31. }
  32. /**
  33. * @param array array of String objects
  34. * @return second to last element in specified array
  35. */ // TODO
  36. public static String getSecondToLastElement(String[] array) {
  37. String word = "";
  38. for(int i = 0; i < array.length -2; i++){
  39. return word = array[array.length-2];
  40. }
  41. return word;
  42. }
  43. /**
  44. * @param array array of String objects
  45. * @param value value to check array for
  46. * @return true if the array contains the specified `value`
  47. */ // TODO
  48. public static boolean contains(String[] array, String value) {
  49. boolean hold = false;
  50. for(String s:array){
  51. if(s.equals(value)){
  52. return hold = true;
  53. }
  54. }
  55. return hold;
  56. }
  57. /**
  58. * @param array of String objects
  59. * @return an array with identical contents in reverse order
  60. */ // TODO
  61. public static String[] reverse(String[] array) {
  62. String[] tempStringArray = new String[array.length];
  63. System.arraycopy( array, 0, tempStringArray, 0, array.length );
  64. for(int i = 0 ;i<tempStringArray.length/2;i++){
  65. String temparray = tempStringArray[i];
  66. tempStringArray[i] = tempStringArray[tempStringArray.length -1 - i];
  67. tempStringArray[tempStringArray.length - 1 - i] = temparray;
  68. }
  69. return tempStringArray;
  70. }
  71. /**
  72. * @param array array of String objects
  73. * @return true if the order of the array is the same backwards and forwards
  74. */ // TODO
  75. public static boolean isPalindromic(String[] array) {
  76. /*
  77. * I'm trying to reverse the order of the array
  78. * than compare if array is equals to array
  79. */
  80. boolean returnPal = false;
  81. String [] reverseArray = reverse(array);
  82. for(int i = 0; i < array.length; i++){
  83. // System.out.println("array[i] " + array[i] + " reverseArray[i] " + reverseArray[i]);
  84. if(array[i].equals(reverseArray[i])){
  85. returnPal = true;
  86. }
  87. }
  88. return returnPal;
  89. }
  90. /**
  91. * @param array array of String objects
  92. * @return true if each letter in the alphabet has been used in the array
  93. */ // TODO
  94. public static boolean isPangramic(String[] array) {
  95. /*int letters = 26;
  96. * if(isPangramic.length() < letters){
  97. * }
  98. */
  99. // int letters = 26;
  100. // boolean pangram = false;
  101. //
  102. // if(array.length < letters){
  103. // pangram = true;
  104. // }
  105. // if(array.length == letters){
  106. // pangram = false;
  107. // }else{
  108. // pangram = true;
  109. // }
  110. // return pangram;
  111. String stringToArray = array.toString();
  112. String str = stringToArray.toLowerCase();
  113. boolean answer = false;
  114. for (char start = 'a';start <='z';++start){
  115. if (!str.contains(String.valueOf(start))){
  116. answer = true;
  117. break;
  118. }
  119. }
  120. return answer;
  121. }
  122. /**
  123. * @param array array of String objects
  124. * @param value value to check array for
  125. * @return number of occurrences the specified `value` has occurred
  126. */ // TODO
  127. public static int getNumberOfOccurrences(String[] array, String value) {
  128. int count = 0;
  129. for(int i = 0;i < array.length;i++){
  130. if(array[i] == value){
  131. count++;
  132. }
  133. }
  134. return count;
  135. }
  136. /**
  137. * @param array array of String objects
  138. * @param valueToRemove value to remove from array
  139. * @return array with identical contents excluding values of `value`
  140. */ // TODO
  141. public static String[] removeValue(String[] array, String valueToRemove) {
  142. StringBuilder sb = new StringBuilder();
  143. //Looping through array
  144. for(int index = 0;index < array.length;index++){
  145. //if the array value does not equal
  146. //the value to remove, then add the values to a temp array
  147. if(array[index] != valueToRemove ){
  148. sb.append(array[index] + "~");
  149. }
  150. }
  151. String answer = sb.toString();
  152. String [] temp = answer.split("~");
  153. return temp;
  154. }
  155. /**
  156. * @param array array of chars
  157. * @return array of Strings with consecutive duplicates removes
  158. */ // TODO
  159. public static String[] removeConsecutiveDuplicates(String[] array) {
  160. //created emptry array with length of array
  161. //created an
  162. String [] answer = new String[array.length];
  163. int i = 0;
  164. answer[i]=array[0];
  165. i++;
  166. for(int j=1;j<array.length;j++){
  167. if(!answer[i-1].equals(array[j])){
  168. answer[i]=array[j];
  169. i++;
  170. }
  171. }
  172. String [] newArray = new String [i];
  173. //copies a source array from a specific beginning position to the destination array
  174. // from the mentioned position.
  175. // No. of arguments to be copied are decided by len argument.
  176. System.arraycopy(answer, 0, newArray, 0, i);
  177. return newArray;
  178. }
  179. public static int counter(String[] array){
  180. int counter = 0;
  181. for(int index = 0; index < array.length-1; index++){
  182. if(array[index].equals(array[index+1])){
  183. counter = counter+=1;
  184. }
  185. }
  186. return counter;
  187. }
  188. /**
  189. * @param array array of chars
  190. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  191. */ // TODO
  192. public static String[] packConsecutiveDuplicates(String[] array) {
  193. StringBuilder pack = new StringBuilder(array[0]);
  194. String [] answer = new String[array.length-counter(array)];
  195. int index = 0;
  196. for(int i=1;i<array.length;i++){
  197. if(array[i-1].equals(array[i])){
  198. pack.append(array[i]);
  199. } else{
  200. answer[index] = pack.toString();
  201. pack.setLength(0);
  202. pack.append(array[i]);
  203. index++;
  204. }
  205. }
  206. answer[index]=pack.toString();
  207. return answer;
  208. }
  209. }