StringArrayUtils.java 4.6KB

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