StringArrayUtils.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. 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(String entry: array){
  41. if(entry.equalsIgnoreCase(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[] reverse = new String[array.length];
  53. int j= 0;
  54. for(int i=array.length-1; i> -1; i--){
  55. reverse[j] = array[i];
  56. j++;
  57. }
  58. return reverse;
  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. String [] reverse = new String[array.length];
  66. reverse = reverse(array);
  67. if(Arrays.equals(array, reverse)){
  68. return true;
  69. }
  70. return false;
  71. }
  72. /**
  73. * @param array array of String objects
  74. * @return true if each letter in the alphabet has been used in the array
  75. */ // TODO
  76. public static boolean isPangramic(String[] array) {
  77. boolean output = false;
  78. char [] 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'};
  79. int count = 0;
  80. for(int i =0; i< array.length; i++){
  81. for(int j =0; j< array[i].length(); j++){
  82. for( int k = 0; k<alphabet.length; k++){
  83. if(array[i].toUpperCase().charAt(j) == (alphabet[k])){
  84. count++;
  85. continue;
  86. }
  87. if(count == 26){
  88. output=true;
  89. break;
  90. }
  91. }
  92. }
  93. }
  94. return output;
  95. }
  96. /**
  97. * @param array array of String objects
  98. * @param value value to check array for
  99. * @return number of occurrences the specified `value` has occurred
  100. */ // TODO
  101. public static int getNumberOfOccurrences(String[] array, String value) {
  102. int count = 0;
  103. for(String entry: array){
  104. if(entry.equals(value)){
  105. count++;
  106. }
  107. }
  108. return count;
  109. }
  110. /**
  111. * @param array array of String objects
  112. * @param valueToRemove value to remove from array
  113. * @return array with identical contents excluding values of `value`
  114. */ // TODO
  115. public static String[] removeValue(String[] array, String valueToRemove) {
  116. int totaltodelete = getNumberOfOccurrences(array,valueToRemove);
  117. String[] output = new String[array.length- totaltodelete];
  118. int count = 0;
  119. for(int i =0; i< array.length; i++){
  120. if(array[i].equals(valueToRemove)){
  121. } else{
  122. output[count] =array[i];
  123. count++;
  124. }
  125. }
  126. return output;
  127. }
  128. /**
  129. * @param array array of chars
  130. * @return array of Strings with consecutive duplicates removes
  131. */ // TODO
  132. public static String[] removeConsecutiveDuplicates(String[] array) {
  133. int count = 0;
  134. int entry = 0;
  135. for(int i =0; i< array.length; i++){
  136. if((i-1>-1) && (array[i].equals(array[i-1]))){
  137. count++;
  138. }
  139. }
  140. String[] output = new String[array.length - count];
  141. for(int i =0; i< array.length; i++){
  142. if((i-1>-1) && (array[i].equals(array[i-1]))){
  143. }else{
  144. output[entry] =array[i];
  145. entry++;
  146. }
  147. }
  148. return output;
  149. }
  150. /**
  151. * @param array array of chars
  152. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  153. */ // TODO
  154. public static String[] packConsecutiveDuplicates(String[] array) {
  155. int count = 0;
  156. int entry = 0;
  157. int dupes = 0;
  158. for(int i =0; i< array.length; i++){
  159. if(((i+1< array.length) && (array[i].equals(array[i+1])))){
  160. } else{
  161. count++;
  162. }
  163. }
  164. String[] output = new String[count];
  165. StringBuilder consecutive = new StringBuilder("");
  166. for(int i =0; i< array.length; i++){
  167. //Is the entry equal to an adjacent entry? If so apeend to consecutive
  168. if((i == array.length -1) || (entry == 0) || (i+1< array.length) && (i-1>-1) && (array[i].equals(array[i-1]) || array[i].equals(array[i+1]))){
  169. consecutive.append(array[i]);
  170. entry++;
  171. if((i == array.length -1) || (i+1< array.length) && !(array[i].equals(array[i+1]))){
  172. output[dupes] = consecutive.toString();
  173. consecutive.setLength(0);
  174. dupes++;
  175. }
  176. }
  177. //If not add it to the array
  178. else{
  179. //but make sure to add consecutive to the array first
  180. if(consecutive.length() > 0){
  181. output[dupes] = array[i];
  182. }else{
  183. output[dupes] = array[i];
  184. }
  185. dupes++;
  186. }
  187. }
  188. System.out.print(output.toString());
  189. return output;
  190. }
  191. }