StringArrayUtils.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * Created by leon on 1/29/18.
  3. */
  4. import java.util.Arrays;
  5. import java.util.*;
  6. public class StringArrayUtils {
  7. /**
  8. * @param array array of String objects
  9. * @return first element of specified array
  10. */ // TODO
  11. public static String getFirstElement(String[] array) {
  12. return array[0];
  13. }
  14. /**
  15. * @param array array of String objects
  16. * @return second element in specified array
  17. */
  18. public static String getSecondElement(String[] array) {
  19. return array[1];
  20. }
  21. /**
  22. * @param array array of String objects
  23. * @return last element in specified array
  24. */ // TODO
  25. public static String getLastElement(String[] array) {
  26. return array[array.length - 1];
  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. return array[array.length - 2];
  34. }
  35. /**
  36. * @param array array of String objects
  37. * @param value value to check array for
  38. * @return true if the array contains the specified `value`
  39. */ // TODO
  40. public static boolean contains(String[] array, String value) {
  41. boolean doesContain = false;
  42. for (String strings : array) {
  43. if (strings == value) {
  44. doesContain = true;
  45. }
  46. }
  47. /* for (int i =0; i < array.length; i++) {
  48. if (array[i] == value) {
  49. doesContain = true;
  50. }
  51. }*/
  52. return doesContain;
  53. }
  54. /**
  55. * @param array of String objects
  56. * @return an array with identical contents in reverse order
  57. */ // TODO
  58. public static String[] reverse(String[] array) {
  59. for (int i = 0; i < array.length / 2; i++) {
  60. String hold = array[i];
  61. array[i] = array[(array.length - 1) - i];
  62. array[array.length - 1 - i] = hold;
  63. }
  64. return array;
  65. }
  66. /**
  67. * @param array array of String objects
  68. * @return true if the order of the array is the same backwards and forwards
  69. */ // TODO
  70. public static boolean isPalindromic(String[] array) {
  71. String [] backwardsArray;
  72. backwardsArray = reverse(array);
  73. boolean isEqual = false;
  74. if(Arrays.equals(array, backwardsArray)) {
  75. isEqual = true;
  76. }
  77. /* for (int i = 0; i < array.length / 2; i++) {
  78. String hold = array[i];
  79. backwardsArray[(array.length - 1) - i] = array[i];
  80. array[array.length - 1 - i] = hold;
  81. hold = backwardsArray[i];
  82. }
  83. boolean isEqual = false;
  84. if(Arrays.equals(array, backwardsArray)) {
  85. isEqual = true;
  86. }*/
  87. return isEqual;
  88. }
  89. /**
  90. * @param array array of String objects
  91. * @return true if each letter in the alphabet has been used in the array
  92. */ // TODO
  93. public static boolean isPangramic(String[] array) {
  94. return false;
  95. }
  96. /**
  97. * @param array array of String objects
  98. * @param value value to check array for
  99. * @return number of occurrences the specified `value` has occurred
  100. */ // TODO
  101. public static int getNumberOfOccurrences(String[] array, String value) {
  102. int occurred = 0;
  103. for (String word : array) {
  104. if (word == value) {
  105. occurred += 1;
  106. }
  107. }
  108. return occurred;
  109. }
  110. /**
  111. * @param array array of String objects
  112. * @param valueToRemove value to remove from array
  113. * @return array with identical contents excluding values of `value`
  114. */ // TODO
  115. public static String[] removeValue(String[] array, String valueToRemove) {
  116. String[] removedArray = new String[array.length];
  117. for (int i = 0; i < array.length ; i++) {
  118. if (array[i] != valueToRemove) {
  119. removedArray[i] = array[i];
  120. }
  121. }
  122. for (String word : array) {
  123. if (word != valueToRemove) {
  124. }
  125. }
  126. return removedArray;
  127. }
  128. /**
  129. * @param array array of chars
  130. * @return array of Strings with consecutive duplicates removes
  131. */ // TODO
  132. public static String[] removeConsecutiveDuplicates(String[] array) {
  133. return null;
  134. }
  135. /**
  136. * @param array array of chars
  137. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  138. */ // TODO
  139. public static String[] packConsecutiveDuplicates(String[] array) {
  140. return null;
  141. }
  142. }