StringArrayUtils.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. return array[array.length-1];
  25. }
  26. /**
  27. * @param array array of String objects
  28. * @return second to last element in specified array
  29. */ // TODO
  30. public static String getSecondToLastElement(String[] array) {
  31. return array[array.length-2];
  32. }
  33. /**
  34. * @param array array of String objects
  35. * @param value value to check array for
  36. * @return true if the array contains the specified `value`
  37. */ // TODO
  38. public static boolean contains(String[] array, String value) {
  39. for(int i = 0; i < array.length; i++) {
  40. if(array[i].equals(value)) return true;
  41. }
  42. return false;
  43. }
  44. /**
  45. * @param array of String objects
  46. * @return an array with identical contents in reverse order
  47. */ // TODO
  48. public static String[] reverse(String[] array) {
  49. String[] revArray = new String[array.length];
  50. for (int i = 0; i < array.length; i++) {
  51. revArray[i] = array[array.length - 1 - i];
  52. }
  53. return revArray;
  54. }
  55. /**
  56. * @param array array of String objects
  57. * @return true if the order of the array is the same backwards and forwards
  58. */ // TODO
  59. public static boolean isPalindromic(String[] array) {
  60. for ( int i = 0; i < array.length / 2; i++) {
  61. if (array[i] != array[array.length - 1 - i]) {
  62. return false;
  63. }
  64. }
  65. return true;
  66. }
  67. /**
  68. * @param array array of String objects
  69. * @return true if each letter in the alphabet has been used in the array
  70. */ // TODO
  71. public static boolean isPangramic(String[] array) {
  72. for(char c = 'a';c <= 'z'; ++c) {
  73. if(array.toString().contains(String.valueOf(c))) {
  74. return true;
  75. }
  76. }return false;
  77. }
  78. /**
  79. * @param array array of String objects
  80. * @param value value to check array for
  81. * @return number of occurrences the specified `value` has occurred
  82. */ // TODO
  83. public static int getNumberOfOccurrences(String[] array, String value) {
  84. int num = 0;
  85. for (int i=0;i<array.length;i++){
  86. if (array[i]== value){
  87. num++;
  88. }
  89. }
  90. return num;
  91. }
  92. /**
  93. * @param array array of String objects
  94. * @param valueToRemove value to remove from array
  95. * @return array with identical contents excluding values of `value`
  96. */ // TODO
  97. public static String[] removeValue(String[] array, String valueToRemove) {
  98. String[] arr = new String[array.length-1];
  99. String[] remArr = new String[1];
  100. for (int i=0;i<array.length;i++){
  101. if (array[i]!=valueToRemove){
  102. arr[i] = array[i];
  103. }else { remArr[i] += array[i];}
  104. }
  105. return arr;
  106. }
  107. /**
  108. * @param array array of chars
  109. * @return array of Strings with consecutive duplicates removes
  110. */ // TODO
  111. public static String[] removeConsecutiveDuplicates(String[] array) {
  112. return null;
  113. }
  114. /**
  115. * @param array array of chars
  116. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  117. */ // TODO
  118. public static String[] packConsecutiveDuplicates(String[] array) {
  119. return null;
  120. }
  121. }