StringArrayUtils.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import java.util.Arrays;
  2. import java.io.*;
  3. import java.util.*;
  4. import java.lang.StringBuilder;
  5. /**
  6. * Created by leon on 1/29/18.
  7. */
  8. public class StringArrayUtils {
  9. /**
  10. * @param array array of String objects
  11. * @return first element of specified array
  12. */ // TODO
  13. public static String getFirstElement(String[] array) {
  14. return array[0];
  15. }
  16. /**
  17. * @param array array of String objects
  18. * @return second element in specified array
  19. */
  20. public static String getSecondElement(String[] array) {
  21. return array[1];
  22. }
  23. /**
  24. * @param array array of String objects
  25. * @return last element in specified array
  26. */ // TODO
  27. public static String getLastElement(String[] array) {
  28. return array[array.length - 1];
  29. }
  30. /**
  31. * @param array array of String objects
  32. * @return second to last element in specified array
  33. */ // TODO
  34. public static String getSecondToLastElement(String[] array) {
  35. return array[array.length - 2];
  36. }
  37. /**
  38. * @param array array of String objects
  39. * @param value value to check array for
  40. * @return true if the array contains the specified `value`
  41. */ // TODO
  42. public static boolean contains(String[] array, String value) {
  43. boolean contains = false;
  44. for(String element : array){
  45. if(element.equals(value)){
  46. contains = true;
  47. }
  48. }
  49. return contains;
  50. }
  51. /**
  52. * @param array of String objects
  53. * @return an array with identical contents in reverse order
  54. */ // TODO
  55. public static String[] reverse(String[] array) {
  56. String[] reverse = new String[array.length];
  57. for (int j=array.length-1, i=0; j>=0; j--, i++){
  58. reverse[i] = array[j];
  59. }
  60. return reverse;
  61. }
  62. /**
  63. * @param array array of String objects
  64. * @return true if the order of the array is the same backwards and forwards
  65. */ // TODO
  66. public static boolean isPalindromic(String[] array) {
  67. boolean isPalindromic = false;
  68. String[] reverse = reverse(array);
  69. if (Arrays.equals(array,reverse)){
  70. isPalindromic = true;
  71. }
  72. return isPalindromic;
  73. }
  74. /**
  75. * @param array array of String objects
  76. * @return true if each letter in the alphabet has been used in the array
  77. */ // TODO
  78. public static boolean isPangramic(String[] array) {
  79. String arrayString = Arrays.toString(array).toLowerCase();
  80. if(arrayString.length() < 26){
  81. return false;
  82. }
  83. for (char c = 'a'; c <='z'; c++){
  84. if(arrayString.indexOf(c) < 0){
  85. return false;
  86. }
  87. }
  88. return true;
  89. }
  90. /**
  91. * @param array array of String objects
  92. * @param value value to check array for
  93. * @return number of occurrences the specified `value` has occurred
  94. */ // TODO
  95. public static int getNumberOfOccurrences(String[] array, String value) {
  96. int numberOfOccurrences = 0;
  97. for (String element : array){
  98. if(element.equals(value)){
  99. numberOfOccurrences += 1;
  100. }
  101. }
  102. return numberOfOccurrences;
  103. }
  104. /**
  105. * @param array array of String objects
  106. * @param valueToRemove value to remove from array
  107. * @return array with identical contents excluding values of `value`
  108. */ // TODO
  109. public static String[] removeValue(String[] array, String valueToRemove) {
  110. int numberOfOccurrences = getNumberOfOccurrences(array, valueToRemove);
  111. String[] newArray = new String[array.length - numberOfOccurrences];
  112. int i = 0;
  113. for(String element : array){
  114. if(!element.equals(valueToRemove)){
  115. newArray[i] = element;
  116. i++;
  117. }
  118. }
  119. return newArray;
  120. }
  121. public static int numberOfConsecutiveDuplicates(String[] array) {
  122. int numberOfConsecDups = 0;
  123. for(int i=1; i<=array.length-1; i++){
  124. if(array[i].equals(array[i-1])){
  125. numberOfConsecDups += 1;
  126. }
  127. }
  128. return numberOfConsecDups;
  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. ArrayList<String> newList = new ArrayList<String>();
  136. //add first element of array to new array
  137. newList.add(array[0]);
  138. //compare each element in your array starting with the second
  139. for (int i=1; i<=array.length-1; i++){
  140. //if an element does not equal an element before it, add to new array
  141. if(!(array[i].equals(array[i-1]))){
  142. newList.add(array[i]);
  143. }
  144. }
  145. return newList.toArray(new String[0]);
  146. }
  147. /**
  148. * @param array array of chars
  149. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  150. */ // TODO
  151. public static String[] packConsecutiveDuplicates(String[] array) {
  152. //create a new string using string builder
  153. StringBuilder arrayAsString = new StringBuilder();
  154. //add first element in array to new array string
  155. arrayAsString.append(array[0]);
  156. //look at each element in the array
  157. for(int i=1; i<=array.length-1; i++){
  158. //if the element in the array equals the previous element, add it to
  159. // the string
  160. if(array[i].equals(array[i-1])){
  161. arrayAsString.append(array[i]);
  162. } else {
  163. //if the element in the array does not equal the previous element,
  164. // add it to the string after a space
  165. arrayAsString.append(" ");
  166. arrayAsString.append(array[i]);
  167. }
  168. }
  169. //convert the new string to a space split string array
  170. String[] packConsecDupArray = arrayAsString.toString().split(" ");
  171. return packConsecDupArray;
  172. }
  173. }