StringArrayUtils.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * Created by leon on 1/29/18.
  3. */
  4. public class StringArrayUtils {
  5. /**
  6. * @param array array of String objects
  7. * @return first element of specified array
  8. */ // TODO
  9. public static String getFirstElement(String[] array) {
  10. return array[0];
  11. }
  12. /**
  13. * @param array array of String objects
  14. * @return second element in specified array
  15. */
  16. public static String getSecondElement(String[] array) {
  17. return array[1];
  18. }
  19. /**
  20. * @param array array of String objects
  21. * @return last element in specified array
  22. */ // TODO
  23. public static String getLastElement(String[] array) {
  24. //String[] array = {"brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  25. String word = "";
  26. for(int i = 0; i < array.length -1; i++){
  27. return word = array[array.length-1];
  28. }
  29. return word;
  30. }
  31. /**
  32. * @param array array of String objects
  33. * @return second to last element in specified array
  34. */ // TODO
  35. public static String getSecondToLastElement(String[] array) {
  36. String word = "";
  37. for(int i = 0; i < array.length -2; i++){
  38. return word = array[array.length-2];
  39. }
  40. return word;
  41. }
  42. /**
  43. * @param array array of String objects
  44. * @param value value to check array for
  45. * @return true if the array contains the specified `value`
  46. */ // TODO
  47. public static boolean contains(String[] array, String value) {
  48. boolean hold = false;
  49. for(String s:array){
  50. if(s.equals(value)){
  51. return hold = true;
  52. }
  53. }
  54. return hold;
  55. }
  56. /**
  57. * @param array of String objects
  58. * @return an array with identical contents in reverse order
  59. */ // TODO
  60. public static String[] reverse(String[] array) {
  61. for(int i = 0 ;i<array.length/2;i++){
  62. String temparray = array[i];
  63. array[i] = array[array.length -1 - i];
  64. array[array.length - 1 - i] = temparray;
  65. }
  66. return array;
  67. }
  68. /**
  69. * @param array array of String objects
  70. * @return true if the order of the array is the same backwards and forwards
  71. */ // TODO
  72. public static boolean isPalindromic(String[] array) {
  73. /*
  74. * I'm trying to reverse the order of the array
  75. * than compare if array is equals to array
  76. */
  77. boolean returnPal = true;
  78. String [] reverseArray = reverse(array);
  79. /*
  80. *
  81. */
  82. for(String i:array){
  83. if (reverseArray.equals(array)){
  84. returnPal = false;
  85. }
  86. }
  87. return returnPal;
  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. /*int letters = 26;
  95. if(isPangramic.length() < letters){
  96. }
  97. *
  98. */
  99. return false;
  100. }
  101. /**
  102. * @param array array of String objects
  103. * @param value value to check array for
  104. * @return number of occurrences the specified `value` has occurred
  105. */ // TODO
  106. public static int getNumberOfOccurrences(String[] array, String value) {
  107. /*
  108. *
  109. */
  110. return 0;
  111. }
  112. /**
  113. * @param array array of String objects
  114. * @param valueToRemove value to remove from array
  115. * @return array with identical contents excluding values of `value`
  116. */ // TODO
  117. public static String[] removeValue(String[] array, String valueToRemove) {
  118. return null;
  119. }
  120. /**
  121. * @param array array of chars
  122. * @return array of Strings with consecutive duplicates removes
  123. */ // TODO
  124. public static String[] removeConsecutiveDuplicates(String[] array) {
  125. return null;
  126. }
  127. /**
  128. * @param array array of chars
  129. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  130. */ // TODO
  131. public static String[] packConsecutiveDuplicates(String[] array) {
  132. return null;
  133. }
  134. }