StringArrayUtils.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. for (String n : array){
  41. if(value == n){
  42. return true;
  43. }
  44. }
  45. return false;
  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. for(int i = 0; i < array.length/2; i++){
  53. String cheese = array[i];
  54. array[i]= array[array.length - i - 1];
  55. array[array.length - i - 1] = cheese;
  56. }
  57. return array;
  58. }
  59. /**
  60. * @param array array of String objects
  61. * @return true if the order of the array is the same backwards and forwards
  62. */ // TODO
  63. public static boolean isPalindromic(String[] array) {
  64. for(int i = 0; i < array.length; i++){
  65. if (array[i] == array[array.length - i - 1]){
  66. return true;
  67. }
  68. }
  69. return false;
  70. }
  71. /**
  72. * @param array array of String objects
  73. * @return true if each letter in the alphabet has been used in the array
  74. */ // TODO
  75. public static boolean isPangramic(String[] array) {
  76. boolean my = true;
  77. for(char alphabet = 'a'; alphabet <= 'z'; alphabet++){
  78. boolean iKnow = false;
  79. for(int i = 0; i < array.length; i++){
  80. if(array[i].toLowerCase().contains(Character.toString(alphabet))){
  81. iKnow = true;
  82. break;
  83. }
  84. }
  85. if(!iKnow){
  86. my = false;
  87. break;
  88. }
  89. }
  90. return my;
  91. }
  92. /**
  93. * @param array array of String objects
  94. * @param value value to check array for
  95. * @return number of occurrences the specified `value` has occurred
  96. */ // TODO
  97. public static int getNumberOfOccurrences(String[] array, String value) {
  98. int count = 0;
  99. for(String word: array){
  100. if(value.equals(word)){
  101. count++;
  102. }
  103. }
  104. return count;
  105. }
  106. /**
  107. * @param array array of String objects
  108. * @param valueToRemove value to remove from array
  109. * @return array with identical contents excluding values of `value`
  110. */ // TODO
  111. public static String[] removeValue(String[] array, String valueToRemove) {
  112. int count = getNumberOfOccurrences(array,valueToRemove);
  113. String[] cheese = new String[array.length - count];
  114. int j = 0;
  115. for(int i = 0; i < array.length; i++){
  116. if(array[i] != valueToRemove){
  117. cheese[j] = array[i];
  118. j++;
  119. }
  120. }
  121. return cheese;
  122. }
  123. /*
  124. * @param array array of chars
  125. * @return array of Strings with consecutive duplicates removes
  126. */ // TODO
  127. public static String[] removeConsecutiveDuplicates(String[] array) {
  128. String removeThese = new String();
  129. int duplicate = getNumberOfOccurrences(array,removeThese);
  130. return null;
  131. }
  132. /**
  133. * @param array array of chars
  134. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  135. */ // TODO
  136. public static String[] packConsecutiveDuplicates(String[] array) {
  137. return null;
  138. }
  139. }