StringArrayUtils.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. 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 answer = false;
  41. for(String x : array){
  42. if(x.equals(value)){
  43. answer = true;
  44. }
  45. }
  46. return answer;
  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. /* StringBuilder x = new StringBuilder(array.toString());
  54. x.reverse();
  55. String[] answer = new String[x.toString().split(" ").length](x.reverse().toString());
  56. */
  57. String[] x = new String[array.length];
  58. int p = 0;
  59. for(int i = array.length-1; i >= 0; i--){
  60. x[p] = array[i];
  61. p++;
  62. }
  63. return x;
  64. }
  65. /**
  66. * @param array array of String objects
  67. * @return true if the order of the array is the same backwards and forwards
  68. */ // TODO
  69. public static boolean isPalindromic(String[] array) {
  70. int x = 0;
  71. int y = array.length - 1;
  72. boolean answer = true;
  73. if(array.length % 2 == 0){
  74. while(x < y && answer == true){
  75. if(array[x] == array[y]){
  76. x++;
  77. y--;
  78. } else{
  79. answer = false;
  80. }
  81. }
  82. } else{
  83. while(x != y && answer == true){
  84. if(array[x] == array[y]){
  85. x++;
  86. y--;
  87. } else{
  88. answer = false;
  89. }
  90. }
  91. }
  92. /* if(reverse(array) != array){
  93. return false;
  94. }
  95. return true;
  96. */
  97. return answer;
  98. }
  99. /**
  100. * @param array array of String objects
  101. * @return true if each letter in the alphabet has been used in the array
  102. */ // TODO
  103. public static boolean isPangramic(String[] array) {
  104. return false;
  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 x : array){
  114. if(x == value){
  115. counter++;
  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. ArrayList<String> x = new ArrayList<String>((Arrays.asList(array)));
  127. for(String h : x){
  128. if(h == valueToRemove){
  129. x.remove(x.indexOf(h));
  130. }
  131. }
  132. String[] q = new String[x.size()];
  133. q = x.toArray(q);
  134. return q;
  135. }
  136. /**
  137. * @param array array of chars
  138. * @return array of Strings with consecutive duplicates removes
  139. */ // TODO
  140. public static String[] removeConsecutiveDuplicates(String[] array) {
  141. ArrayList<String> x = new ArrayList<String>();
  142. // String[] y = new String[];
  143. for(int i = 0; i < array.length; i++){
  144. if(array[i] == array[i + 1]){
  145. x.add(array[i]);
  146. }
  147. }
  148. String[] y = new String[x.size()];
  149. x.toArray(y);
  150. return y;
  151. }
  152. /**
  153. * @param array array of chars
  154. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  155. */ // TODO
  156. public static String[] packConsecutiveDuplicates(String[] array) {
  157. return null;
  158. }
  159. }