StringArrayUtils.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. /**
  5. * Created by leon on 1/29/18.
  6. */
  7. public class StringArrayUtils {
  8. /**
  9. * @param array array of String objects
  10. * @return first element of specified array
  11. */ // TODO
  12. public static String getFirstElement(String[] array) {
  13. return array[0];
  14. }
  15. /**
  16. * @param array array of String objects
  17. * @return second element in specified array
  18. */
  19. public static String getSecondElement(String[] array) {
  20. return array[1];
  21. }
  22. /**
  23. * @param array array of String objects
  24. * @return last element in specified array
  25. */ // TODO
  26. public static String getLastElement(String[] array) {
  27. return array[array.length - 1];
  28. }
  29. /**
  30. * @param array array of String objects
  31. * @return second to last element in specified array
  32. */ // TODO
  33. public static String getSecondToLastElement(String[] array) {
  34. return array[array.length - 2];
  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 (String ithString : array) {
  43. if (ithString.equals(value)) {
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49. /**
  50. * @param array of String objects
  51. * @return an array with identical contents in reverse order
  52. */ // TODO
  53. public static String[] reverse(String[] array) {
  54. String[] reverse = new String[array.length];
  55. for (int i = 0; i < array.length; i++) {
  56. reverse[i] = array[array.length - 1 - i];
  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 = reverse(array);
  66. for (int i = 0; i < array.length; i++) {
  67. if (!array[i].equals(reverse[i])) {
  68. return false;
  69. }
  70. }
  71. return true;
  72. }
  73. /**
  74. * @param array array of String objects
  75. * @return true if each letter in the alphabet has been used in the array
  76. */ // TODO
  77. public static boolean isPangramic(String[] array) {
  78. //stringbuilder holding all letters of alpha
  79. StringBuilder alphabet = new StringBuilder("abcdefghijklmnopqrstuvwxyz");
  80. //iterate over each word of the array and then each char of word
  81. for (String word : array) {
  82. for (int i = 0; i < word.length(); i++) {
  83. int index = alphabet.indexOf("" + word.toLowerCase().charAt(i));
  84. if (index >= 0) {
  85. alphabet.deleteCharAt(index);
  86. }
  87. if (alphabet.length() == 0) {
  88. return true;
  89. }
  90. }
  91. }
  92. return false;
  93. }
  94. /**
  95. * @param array array of String objects
  96. * @param value value to check array for
  97. * @return number of occurrences the specified `value` has occurred
  98. */ // TODO
  99. public static int getNumberOfOccurrences(String[] array, String value) {
  100. int occurrences = 0;
  101. for (String word : array) {
  102. if (value.equals(word)) {
  103. occurrences++;
  104. }
  105. }
  106. return occurrences;
  107. }
  108. /**
  109. * @param array array of String objects
  110. * @param valueToRemove value to remove from array
  111. * @return array with identical contents excluding values of `value`
  112. */ // TODO
  113. public static String[] removeValue(String[] array, String valueToRemove) {
  114. List<String> arrRemoved = new ArrayList<String>();
  115. for (String word : array) {
  116. if (!word.equals(valueToRemove)) {
  117. arrRemoved.add(word);
  118. }
  119. }
  120. String[] removedArray = new String[arrRemoved.size()];
  121. removedArray = arrRemoved.toArray(removedArray);
  122. return removedArray;
  123. }
  124. /**
  125. * @param array array of chars
  126. * @return array of Strings with consecutive duplicates removes
  127. */ // TODO
  128. public static String[] removeConsecutiveDuplicates(String[] array) {
  129. List<String> removedValues = new ArrayList<String>();
  130. for (String word : array) {
  131. //empty or last added value doesnt equal iterated word of array
  132. if (removedValues.isEmpty() ||
  133. !removedValues.get(removedValues.size() - 1).equals(word)) {
  134. removedValues.add(word);
  135. }
  136. }
  137. String[] nonDuplicate = new String[removedValues.size()];
  138. nonDuplicate = removedValues.toArray(nonDuplicate);
  139. return nonDuplicate;
  140. }
  141. /**
  142. * @param array array of chars
  143. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  144. */ // TODO
  145. public static String[] packConsecutiveDuplicates(String[] array) {
  146. for (String singleChar : array) {
  147. }
  148. return null;
  149. }
  150. }