StringArrayUtils.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. * Created by leon on 1/29/18.
  3. */
  4. import java.util.Arrays;
  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. String first = "";
  12. first += array[0];
  13. return first;
  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. String second = "";
  21. second += array[1];
  22. return second;
  23. }
  24. /**
  25. * @param array array of String objects
  26. * @return last element in specified array
  27. */ // TODO
  28. public static String getLastElement(String[] array) {
  29. String last = "";
  30. last += array[array.length-1];
  31. return last;
  32. }
  33. /**
  34. * @param array array of String objects
  35. * @return second to last element in specified array
  36. */ // TODO
  37. public static String getSecondToLastElement(String[] array) {
  38. String secLast = "";
  39. secLast += array[array.length-2];
  40. return secLast;
  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 contain = false;
  49. for(String s : array){
  50. if(s==value){
  51. contain = true;
  52. }
  53. }
  54. return contain;
  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 reversal;
  62. for(int i = 0; i<array.length/2; i++){
  63. reversal = array[i];
  64. array[i] = array[array.length - i -1];
  65. array[array.length - i -1] = reversal;
  66. }
  67. return array;
  68. }
  69. /**
  70. * @param array array of String objects
  71. * @return true if the order of the array is the same backwards and forwards
  72. */ // TODO
  73. public static boolean isPalindromic(String[] array) {
  74. boolean pal = false;
  75. String reversal;
  76. for(int i = 0; i<array.length/2; i++){
  77. if(array[i] == array[array.length - i -1]){
  78. pal = true;
  79. }
  80. }
  81. return pal;
  82. }
  83. /**
  84. * @param array array of String objects
  85. * @return true if each letter in the alphabet has been used in the array
  86. */ // TODO
  87. public static boolean isPangramic(String[] array) {
  88. boolean pan = false;
  89. StringBuilder build = new StringBuilder();
  90. for(String s : array){
  91. build.append(s);
  92. }
  93. String ofArray = "";
  94. ofArray = build.toString();
  95. for(char i = 'A'; i <= 'Z'; i++){
  96. if((ofArray.indexOf(i) < 0) && (ofArray.indexOf((char)(i+32)) < 0))
  97. { // goes thru the ASCII list
  98. pan = false;
  99. }
  100. else{
  101. pan = true;
  102. }
  103. }
  104. return pan;
  105. }
  106. /**
  107. * @param array array of String objects
  108. * @param value value to check array for
  109. * @return number of occurrences the specified `value` has occurred
  110. */ // TODO
  111. public static int getNumberOfOccurrences(String[] array, String value) {
  112. int occurs = 0;
  113. for(int i=0; i<array.length; i++){
  114. if(value.equals(array[i])){
  115. occurs += 1 ;
  116. }
  117. }
  118. return occurs;
  119. }
  120. /**
  121. * @param array array of String objects
  122. * @param valueToRemove value to remove from array
  123. * @return array with identical contents excluding values of `value`
  124. */ // TODO
  125. public static String[] removeValue(String[] array, String valueToRemove) {
  126. int count = 0;
  127. int occur = getNumberOfOccurrences(array, valueToRemove);
  128. String[] exclude = new String[array.length - occur];
  129. for(String i : array){
  130. if(i != valueToRemove){
  131. exclude[count] = i;
  132. count++;
  133. }
  134. }
  135. return exclude;
  136. }
  137. /**
  138. * @param array array of chars
  139. * @return array of Strings with consecutive duplicates removes
  140. */ // TODO
  141. public static String[] removeConsecutiveDuplicates(String[] array) {
  142. String[] excluded = new String[array.length];
  143. int newArrIndex = 0;
  144. excluded[newArrIndex] = array[0];
  145. newArrIndex++;
  146. for(int i = 1; i<array.length; i++){
  147. if(!(excluded[newArrIndex-1]).equals(array[i])){
  148. excluded[newArrIndex] = array[i];
  149. newArrIndex++;
  150. }
  151. }
  152. String[] newArr = new String[newArrIndex];
  153. System.arraycopy(excluded, 0, newArr, 0, newArrIndex);
  154. return newArr;
  155. }
  156. /**
  157. * @param array array of chars
  158. * @return array of Strings with each consecutive duplicate
  159. * occurrence concatenated as a single string in an array of Strings
  160. */ // TODO
  161. public static String[] packConsecutiveDuplicates(String[] array) {
  162. String[] newArr = new String[array.length];
  163. return null;
  164. }
  165. }