StringArrayUtils.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import java.util.*;
  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 lastStr = array[array.length - 1];
  26. return lastStr;
  27. }
  28. /**
  29. * @param array array of String objects
  30. * @return second to last element in specified array
  31. */ // TODO
  32. public static String getSecondToLastElement(String[] array) {
  33. String lastStr = array[array.length - 2];
  34. return lastStr;
  35. }
  36. /**
  37. * @param array array of String objects
  38. * @param value value to check array for
  39. * @return true if the array contains the specified `value`
  40. */ // TODO
  41. public static boolean contains(String[] array, String value) {
  42. for (int i = 0; i < array.length; i++){
  43. if (array[i] == value) {
  44. String result = array[i];
  45. }
  46. return true;
  47. }
  48. return false;
  49. }
  50. /**
  51. * @param array of String objects
  52. * @return an array with identical contents in reverse order
  53. */ // TODO
  54. public static String[] reverse(String[] array) {
  55. for (int i = 0; i < array.length/2; i++){
  56. String temp = array[i];
  57. array[i] = array[array.length - i - 1];
  58. array[array.length - i - 1] = temp;
  59. }
  60. return array;
  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. int i1 = 0;
  68. int i2 = array.length - 1;
  69. while (i2 > i1) {
  70. if (array[i1] != array[i2]) {
  71. return false;
  72. }
  73. ++i1;
  74. --i2;
  75. }
  76. return true;
  77. }
  78. /**
  79. * @param array array of String objects
  80. * @return true if each letter in the alphabet has been used in the array
  81. */ // TODO
  82. public static boolean isPangramic(String[] array) {
  83. boolean bool = true;
  84. for (char letter = 'a'; letter <= 'z'; letter++){
  85. boolean bool2 = false;
  86. for (int i = 0; i < array.length; i++){
  87. if (array[i].toLowerCase().contains(Character.toString(letter))){
  88. bool2 = true;
  89. }
  90. }
  91. if (!bool2) {
  92. bool = false;
  93. }
  94. }
  95. return bool;
  96. }
  97. /**
  98. * @param array array of String objects
  99. * @param value value to check array for
  100. * @return number of occurrences the specified `value` has occurred
  101. */ // TODO
  102. public static int getNumberOfOccurrences(String[] array, String value) {
  103. int counter = 0;
  104. for (String word : array) {
  105. if (value.equals(word)){
  106. counter++;
  107. }
  108. }
  109. return counter;
  110. }
  111. /**
  112. * @param array array of String objects
  113. * @param valueToRemove value to remove from array
  114. * @return array with identical contents excluding values of `value`
  115. */ // TODO
  116. public static String[] removeValue(String[] array, String valueToRemove) {
  117. ArrayList<String> strArr = new ArrayList<String> (Arrays.asList(array));
  118. strArr.remove(valueToRemove);
  119. String[] newArr = strArr.toArray(new String[strArr.size()]);
  120. return newArr;
  121. }
  122. /**
  123. * @param array array of chars
  124. * @return array of Strings with consecutive duplicates removes
  125. */ // TODO
  126. public static String[] removeConsecutiveDuplicates(String[] array) {
  127. int duplicate = 0;
  128. int counter = 0;
  129. for (int i = 0; i < array.length - 1 ; i++){
  130. if (array[i].equals(array[i+1])){
  131. counter++;
  132. }
  133. }
  134. String[] newArr = new String[array.length - counter];
  135. String str = null;
  136. for (int i = 0; i < array.length; i++){
  137. if(!array[i].equals(str)){
  138. newArr[duplicate] = array[i];
  139. str = array[i];
  140. duplicate++;
  141. }
  142. }
  143. return newArr;
  144. }
  145. /**
  146. * @param array array of chars
  147. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  148. */ // TODO
  149. public static String[] packConsecutiveDuplicates(String[] array) {
  150. int counter = getCounter(array);
  151. String[] result = new String[array.length - counter];
  152. String last = array[0];
  153. result[0] = array[0];
  154. int index = 0;
  155. for (int i = 1; i < array.length; i++){
  156. if (array[i].equals(last)){
  157. result[index] += array[i];
  158. last = array[i];
  159. }
  160. else {
  161. index++;
  162. result[index] = array[i];
  163. last = array[i];
  164. }
  165. }
  166. return result;
  167. }
  168. public static int getCounter(String[] array){
  169. int counter = 0;
  170. for(int i = 0; i < array.length - 1; i++){
  171. if(array[i].equals(array[i+1])){
  172. counter++;
  173. }
  174. }
  175. return counter;
  176. }
  177. }