StringArrayUtils.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * Created by leon on 1/29/18.
  3. */
  4. import java.util.Arrays;
  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. String result = "";
  12. for(int i = 0; i < array.length; i++) {
  13. result += array[0];
  14. break;
  15. }
  16. return result;
  17. }
  18. /**
  19. * @param array array of String objects
  20. * @return second element in specified array
  21. */
  22. public static String getSecondElement(String[] array) {
  23. String result = "";
  24. for(int i = 0; i < array.length; i++) {
  25. result += array[1];
  26. break;
  27. }
  28. return result;
  29. }
  30. /**
  31. * @param array array of String objects
  32. * @return last element in specified array
  33. */ // TODO
  34. public static String getLastElement(String[] array) {
  35. return array[array.length -1];
  36. }
  37. /**
  38. * @param array array of String objects
  39. * @return second to last element in specified array
  40. */ // TODO
  41. public static String getSecondToLastElement(String[] array) {
  42. return array[array.length -2];
  43. }
  44. /**
  45. * @param array array of String objects
  46. * @param value value to check array for
  47. * @return true if the array contains the specified `value`
  48. */ // TODO
  49. public static boolean contains(String[] array, String value) {
  50. boolean hasString = false;
  51. for(String s : array) {
  52. if(s.equals(value)) {
  53. hasString = true;
  54. break;
  55. }
  56. }
  57. return hasString;
  58. }
  59. /**
  60. * @param array of String objects
  61. * @return an array with identical contents in reverse order
  62. */ // TODO
  63. public static String[] reverse(String[] array) {
  64. String[] ns = new String[array.length];
  65. for(int i = 0; i < array.length; i++){
  66. ns[array.length -1 -i] = array[i];
  67. }
  68. return ns;
  69. }
  70. /**
  71. * @param array array of String objects
  72. * @return true if the order of the array is the same backwards and forwards
  73. */ // TODO
  74. public static boolean isPalindromic(String[] array) {
  75. boolean ispalindromin = false;
  76. for(int i = 0; i < array.length / 2; i++) {
  77. if(array[i] == array[array.length -i -1]) {
  78. ispalindromin = true;
  79. }
  80. }
  81. return ispalindromin;
  82. }
  83. /**
  84. * @param array array of String objects
  85. * @return true if each letter in the alphabet has been used in the array
  86. */ // TODO
  87. public static boolean isPangramic(String[] array) {
  88. boolean ispangramic = false;
  89. for(char i = 'A'; i <= 'Z'; i++) {
  90. if(Arrays.toString(array).indexOf(i) < 0 && (Arrays.toString(array).indexOf((char) i + 32 ) < 0)) {
  91. ispangramic = false;
  92. break;
  93. } else {
  94. ispangramic = true;
  95. }
  96. }
  97. return ispangramic;
  98. }
  99. /**
  100. * @param array array of String objects
  101. * @param value value to check array for
  102. * @return number of occurrences the specified `value` has occurred
  103. */ // TODO
  104. public static int getNumberOfOccurrences(String[] array, String value) {
  105. int count = 0;
  106. for(String s : array){
  107. if(s.equals(value)){
  108. count +=1;
  109. }
  110. }
  111. return count;
  112. }
  113. /**
  114. * @param array array of String objects
  115. * @param valueToRemove value to remove from array
  116. * @return array with identical contents excluding values of `value`
  117. */ // TODO
  118. public static String[] removeValue(String[] array, String valueToRemove) {
  119. int count = 0;
  120. int occurences = getNumberOfOccurrences(array, valueToRemove);
  121. String[] str = new String[array.length - occurences];
  122. for(String element : array) {
  123. if(element != valueToRemove) {
  124. str[count] = element;
  125. count++;
  126. }
  127. }
  128. return str;
  129. }
  130. /**
  131. * @param array array of chars
  132. * @return array of Strings with consecutive duplicates removes
  133. */ // TODO
  134. public static String[] removeConsecutiveDuplicates(String[] array) {
  135. int dupCount = 0;
  136. for(int i = 0; i < array.length -1; i++) {
  137. if(array[i].equals(array[i+1])) {
  138. dupCount = dupCount + 1;
  139. }
  140. }
  141. String[] str = new String[array.length - dupCount];
  142. int j = 0;
  143. for(int i = 0; i < array.length -1; i++) {
  144. if(!array[i].equals(array[i + 1])) {
  145. str[j++] = (array[i]);
  146. }
  147. }
  148. str[str.length -1] = array[array.length -1];
  149. return str;
  150. }
  151. /**
  152. * @param array array of chars
  153. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  154. */ // TODO
  155. public static String[] packConsecutiveDuplicates(String[] array) {
  156. int dupCount = 0;
  157. for(int i = 0; i < array.length-1; i++){
  158. if(array[i].equals(array[i+1])){
  159. dupCount = dupCount + 1;
  160. }
  161. }
  162. String[] str = new String[array.length - dupCount];
  163. int j = 0;
  164. for(int i = 0; i < array.length-1; i++){
  165. if(array[i].equals(array[i+1])){
  166. StringBuilder sb = new StringBuilder(array[i]);
  167. sb.append(array[i+1]);
  168. str[j++] = (array[i]);
  169. }
  170. }
  171. return str;
  172. }
  173. }