StringArrayUtils.java 5.1KB

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