StringArrayUtils.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import java.util.List;
  2. import java.util.Arrays;
  3. /**
  4. * Created by leon on 1/29/18.
  5. */
  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. List<String> random = Arrays.asList("the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog");
  13. String first = random.get(0);
  14. return first;
  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. List <String> random = Arrays.asList("the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog");
  22. String second = random.get(1);
  23. return second;
  24. }
  25. /**
  26. * @param array array of String objects
  27. * @return last element in specified array
  28. */ // TODO
  29. public static String getLastElement(String[] array) {
  30. List <String> random = Arrays.asList("the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog");
  31. String last = random.get(array.length-1);
  32. return last;
  33. }
  34. /**
  35. * @param array array of String objects
  36. * @return second to last element in specified array
  37. */ // TODO
  38. public static String getSecondToLastElement(String[] array) {
  39. List <String> random = Arrays.asList("the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog");
  40. String secondToLast = random.get(array.length-2);
  41. return secondToLast;
  42. }
  43. /**
  44. * @param array array of String objects
  45. * @param value value to check array for
  46. * @return true if the array contains the specified `value`
  47. */ // TODO
  48. public static boolean contains(String[] array, String value) {
  49. String[] arr = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  50. if (Arrays.asList(arr).contains("quick") == true){
  51. return true;
  52. }
  53. else{
  54. }
  55. return false ;
  56. }
  57. /**
  58. * @param array of String objects
  59. * @return an array with identical contents in reverse order
  60. */ // TODO
  61. public static String[] reverse(String[] array) {
  62. for (int i=0; i<array.length/2;i++){
  63. String temp = array[i];
  64. array[i] = array[array.length-1-i];
  65. array[array.length-1-i] = temp;
  66. }
  67. return array;
  68. }
  69. /**
  70. * @param array array of String objects
  71. * @return true if the order of the array is the same backwards and forwards
  72. */ // TODO
  73. public static boolean isPalindromic(String[] array) {
  74. String[] newArr = new String[array.length]; //newArr is a copy of original array
  75. //newArray[i] = array[array.length-i-1];
  76. for (int i=0;i<array.length;i++){
  77. newArr[i]=array[array.length-i-1];
  78. }
  79. if (Arrays.equals(newArr,array)){
  80. return true;
  81. }
  82. return false;
  83. // if (array.length%2 ==0){
  84. // for (int i=0; i<array.length/2;i++){
  85. // String temp = array[i];
  86. // array[i] = array[array.length-1];
  87. // array[array.length-1] = temp;
  88. // //reverse the original array
  89. // }
  90. // }
  91. // if (Arrays.equals(newArr,array)){
  92. // return true;
  93. // }
  94. // // else if(!array[0].equals(array[array.length-1])){
  95. // // return false;
  96. // // }
  97. // else{
  98. // return false;
  99. // }
  100. }
  101. /**
  102. * @param array array of String objects
  103. * @return true if each letter in the alphabet has been used in the array
  104. */ // TODO
  105. public static boolean isPangramic(String[] array) {
  106. return false;
  107. }
  108. /**
  109. * @param array array of String objects
  110. * @param value value to check array for
  111. * @return number of occurrences the specified `value` has occurred
  112. */ // TODO
  113. public static int getNumberOfOccurrences(String[] array, String value) {
  114. return 0;
  115. }
  116. /**
  117. * @param array array of String objects
  118. * @param valueToRemove value to remove from array
  119. * @return array with identical contents excluding values of `value`
  120. */ // TODO
  121. public static String[] removeValue(String[] array, String valueToRemove) {
  122. return null;
  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. return null;
  130. }
  131. /**
  132. * @param array array of chars
  133. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  134. */ // TODO
  135. public static String[] packConsecutiveDuplicates(String[] array) {
  136. return null;
  137. }
  138. }