StringArrayUtils.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. boolean containsString = false;
  41. for (String element : array)
  42. if(element.equals(value)){
  43. containsString = true;
  44. break;
  45. }
  46. return containsString;
  47. }
  48. /**
  49. * @param array of String objects
  50. * @return an array with identical contents in reverse order
  51. */ // TODO
  52. public static String[] reverse(String[] array) {
  53. String[] reverseString = new String[array.length];
  54. for(int i = 0; i < array.length; i++){
  55. reverseString [array.length-1-i] = array[i];
  56. }
  57. return reverseString;
  58. }
  59. /**
  60. * @param array array of String objects
  61. * @return true if the order of the array is the same backwards and forwards
  62. */ // TODO
  63. public static boolean isPalindromic(String[] array) {
  64. String[] reverseString = reverse(array);
  65. boolean isTrue = false;
  66. if(Arrays.equals(reverseString, array)){
  67. return true;
  68. }
  69. return isTrue;
  70. }
  71. /**
  72. * @param array array of String objects
  73. * @return true if each letter in the alphabet has been used in the array
  74. */ // TODO
  75. public static boolean isPangramic(String[] array){
  76. 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'};
  77. boolean containsEveryLetter = true;
  78. for(char letter:alphabet){
  79. if(arrayContainsLetter(array,letter)==false){
  80. containsEveryLetter = false;
  81. break;
  82. }
  83. }
  84. return containsEveryLetter;
  85. }
  86. public static boolean arrayContainsLetter(String[] array, char letter){
  87. boolean containsLetter = false;
  88. for(String string : array){
  89. if(stringContainsLetter(string.toLowerCase(),letter)){
  90. containsLetter = true;
  91. break;
  92. }
  93. }
  94. return containsLetter;
  95. }
  96. public static boolean stringContainsLetter(String string, char letter){
  97. boolean containsLetter = false;
  98. for(int i = 0; i < string.length(); i++){
  99. if (string.charAt(i)==letter){
  100. containsLetter = true;
  101. break;
  102. }
  103. }
  104. return containsLetter;
  105. }
  106. /**
  107. * @param array array of String objects
  108. * @param value value to check array for
  109. * @return number of occurrences the specified `value` has occurred
  110. */ // TODO
  111. public static int getNumberOfOccurrences(String[] array, String value) {
  112. int counter = 0;
  113. for (String element : array){
  114. if(element.equals(value)){
  115. counter += 1;
  116. }
  117. }
  118. return counter;
  119. }
  120. /**
  121. * @param array array of String objects
  122. * @param valueToRemove value to remove from array
  123. * @return array with identical contents excluding values of `value`
  124. */ // TODO
  125. public static String[] removeValue(String[] array, String valueToRemove) {
  126. int lengthOfNewArray = array.length-getNumberOfOccurrences(array,valueToRemove);
  127. String[] newArray = new String[lengthOfNewArray];
  128. int index = 0;
  129. for(String element : array){
  130. if(element != valueToRemove){
  131. newArray[index] = element;
  132. index += 1;
  133. }
  134. }
  135. return newArray;
  136. }
  137. /**
  138. * @param array array of chars
  139. * @return array of Strings with consecutive duplicates removes
  140. */ // TODO
  141. public static String[] removeConsecutiveDuplicates(String[] array) {
  142. int newArrayLength = array.length - countDuplicates(array);
  143. String[] newArray = new String[newArrayLength];
  144. newArray[0] = array[0];
  145. int index = 1;
  146. for(int i = 1; i < array.length; i++){
  147. if(!array[i].equals((array[i-1]))){
  148. newArray[index] = array[i];
  149. index += 1;
  150. }
  151. }
  152. return newArray;
  153. }
  154. public static int countDuplicates(String[] array){
  155. int duplicates = 0;
  156. for (int i =1; i< array.length; i++){
  157. if(array[i].equals(array[i-1])){
  158. duplicates++;
  159. }
  160. }
  161. return duplicates;
  162. }
  163. /**
  164. * @param array array of chars
  165. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  166. */ // TODO
  167. public static String[] packConsecutiveDuplicates(String[] array) {
  168. int newArrayLength = array.length-countDuplicates(array);
  169. String [] newArray = new String [newArrayLength];
  170. StringBuilder tempString = new StringBuilder(array[0]);
  171. int index = 0;
  172. for (int i = 1; i< array.length; i++){
  173. if(array[i].equals(array[i-1])){
  174. tempString.append(array[i]);
  175. }else{
  176. newArray[index] = tempString.toString();
  177. index++;
  178. tempString.delete(0,tempString.length());
  179. tempString.append(array[i]);
  180. }
  181. }
  182. newArray[index] = tempString.toString();
  183. return newArray;
  184. }
  185. }