StringArrayUtils.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. String [] t = new String [array.length];
  62. for(int i = 0 ;i<array.length;i++){
  63. t[i] = "";
  64. for(int j = array[i].length()-1; j>= 0;j--){
  65. t[i] +=array[i].substring(j,j+1);
  66. }
  67. }
  68. return t;
  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 length = array.length;
  76. for(int i = 0;i<length;i++){
  77. String start = array[0];
  78. String end = array[length];
  79. }
  80. return false;
  81. }
  82. /**
  83. * @param array array of String objects
  84. * @return true if each letter in the alphabet has been used in the array
  85. */ // TODO
  86. public static boolean isPangramic(String[] array) {
  87. return false;
  88. }
  89. /**
  90. * @param array array of String objects
  91. * @param value value to check array for
  92. * @return number of occurrences the specified `value` has occurred
  93. */ // TODO
  94. public static int getNumberOfOccurrences(String[] array, String value) {
  95. return 0;
  96. }
  97. /**
  98. * @param array array of String objects
  99. * @param valueToRemove value to remove from array
  100. * @return array with identical contents excluding values of `value`
  101. */ // TODO
  102. public static String[] removeValue(String[] array, String valueToRemove) {
  103. return null;
  104. }
  105. /**
  106. * @param array array of chars
  107. * @return array of Strings with consecutive duplicates removes
  108. */ // TODO
  109. public static String[] removeConsecutiveDuplicates(String[] array) {
  110. return null;
  111. }
  112. /**
  113. * @param array array of chars
  114. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  115. */ // TODO
  116. public static String[] packConsecutiveDuplicates(String[] array) {
  117. return null;
  118. }
  119. }