123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import java.util.Arrays;
  2. /**
  3. * Created by leon on 1/29/18.
  4. */
  5. public class StringArrayUtils {
  6. /**
  7. * @param array array of String objects
  8. * @return first element of specified array
  9. */ // TODO
  10. public static String getFirstElement(String[] array) {
  11. return array[0];
  12. }
  13. /**
  14. * @param array array of String objects
  15. * @return second element in specified array
  16. */
  17. public static String getSecondElement(String[] array) {
  18. return array[1];
  19. }
  20. /**
  21. * @param array array of String objects
  22. * @return last element in specified array
  23. */ // TODO
  24. public static String getLastElement(String[] array) {
  25. return array[array.length-1];
  26. }
  27. /**
  28. * @param array array of String objects
  29. * @return second to last element in specified array
  30. */ // TODO
  31. public static String getSecondToLastElement(String[] array) {
  32. return array[array.length-2];
  33. }
  34. /**
  35. * @param array array of String objects
  36. * @param value value to check array for
  37. * @return true if the array contains the specified `value`
  38. */ // TODO
  39. public static boolean contains(String[] array, String value) {
  40. boolean answer = false;
  41. for (String string : array) {
  42. if (string.equalsIgnoreCase(value));
  43. answer = true;
  44. }
  45. return answer;
  46. }
  47. /**
  48. * @param array of String objects
  49. * @return an array with identical contents in reverse order
  50. */ // TODO
  51. public static String[] reverse(String[] array) {
  52. String[] answer = new String[array.length];
  53. for (int i = 0; i< array.length; i++)
  54. answer[i] = array[array.length-1-i];
  55. return answer;
  56. }
  57. /**
  58. * @param array array of String objects
  59. * @return true if the order of the array is the same backwards and forwards
  60. */ // TODO
  61. public static boolean isPalindromic(String[] array) {
  62. boolean answer = false;
  63. for (int i = 0; i < array.length; i++)
  64. if (Arrays.equals(array, reverse(array))){
  65. answer = true;
  66. }
  67. return answer;
  68. }
  69. /**
  70. * @param array array of String objects
  71. * @return true if each letter in the alphabet has been used in the array
  72. */ // TODO
  73. public static boolean isPangramic(String[] array) {
  74. boolean isPangramic = false;
  75. for (char i = 'A' ; i <= 'Z'; i++)
  76. if (Arrays.toString(array).indexOf(i) < 0 && (Arrays.toString(array).indexOf((char) i +32) < 0)) {
  77. isPangramic = false;
  78. break;
  79. } else {
  80. isPangramic = true;
  81. }
  82. return isPangramic;
  83. }
  84. /**
  85. * @param array array of String objects
  86. * @param value value to check array for
  87. * @return number of occurrences the specified `value` has occurred
  88. */ // TODO
  89. public static int getNumberOfOccurrences(String[] array, String value) {
  90. int count = 0;
  91. for (String s: array) {
  92. if (value.equals(s)) {
  93. count ++;
  94. }
  95. }
  96. return count;
  97. }
  98. /**
  99. * @param array array of String objects
  100. * @param valueToRemove value to remove from array
  101. * @return array with identical contents excluding values of `value`
  102. */ // TODO
  103. public static String[] removeValue(String[] array, String valueToRemove) {
  104. int index = 0;
  105. int count = getNumberOfOccurrences(array, valueToRemove);
  106. String[] newArray = new String[array.length-count];
  107. for (int i = 0; i < array.length; i++) {
  108. if (array[i] != valueToRemove) {
  109. newArray[index] = array[i];
  110. index++;
  111. }
  112. }
  113. return newArray;
  114. }
  115. /**
  116. * @param array array of chars
  117. * @return array of Strings with consecutive duplicates removes
  118. */ // TODO
  119. public static String[] removeConsecutiveDuplicates(String[] array) {
  120. return null;
  121. }
  122. /**
  123. * @param array array of chars
  124. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  125. */ // TODO
  126. public static String[] packConsecutiveDuplicates(String[] array) {
  127. return null;
  128. }
  129. }