StringArrayUtils.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import java.util.List;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  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. for(String string : array)
  43. if(string.equalsIgnoreCase(value)){
  44. return true;
  45. }
  46. return false;
  47. }
  48. // for(int i=0;i<array.length;i++){
  49. // if(array[i]== value){
  50. // return true;
  51. // }
  52. // }
  53. // return false;
  54. //}
  55. /**
  56. * @param array of String objects
  57. * @return an array with identical contents in reverse order
  58. */ // TODO
  59. public static String[] reverse(String[] array) {
  60. String[] answer = new String[array.length];
  61. for(int i =0; i<array.length; i++)
  62. answer[i] = array[array.length-1-i];
  63. return answer;
  64. }
  65. /**
  66. * @param array array of String objects
  67. * @return true if the order of the array is the same backwards and forwards
  68. */ // TODO
  69. public static boolean isPalindromic(String[] array) {
  70. boolean answer = false;
  71. if(Arrays.equals(array,reverse(array))){
  72. answer=true;
  73. }
  74. return answer;
  75. }
  76. /**
  77. * @param array array of String objects
  78. * @return true if each letter in the alphabet has been used in the array
  79. */ // TODO
  80. public static boolean isPangramic(String[] array) {
  81. boolean isPangramic = false;
  82. for(char i = 'A'; i <= 'Z'; i++) {
  83. if(Arrays.toString(array).indexOf(i) < 0 && (Arrays.toString(array).indexOf((char) i + 32) < 0)) {
  84. isPangramic = false;
  85. }else{
  86. isPangramic = true;
  87. }
  88. }
  89. return isPangramic;
  90. }
  91. /**
  92. * @param array array of String objects
  93. * @param value value to check array for
  94. * @return number of occurrences the specified `value` has occurred
  95. */ // TODO
  96. public static int getNumberOfOccurrences(String[] array, String value) {
  97. //int count = 0;
  98. //for(String s : array){
  99. //if(s.equals(value)){
  100. //count +=1;
  101. //}
  102. //}
  103. //return count;
  104. //}
  105. int count=0;
  106. for(int i =0;i<array.length; i++){
  107. if(array[i].equals(value)){
  108. count++;
  109. }
  110. }
  111. return count;
  112. }
  113. /**
  114. * @param array array of String objects
  115. * @param valueToRemove value to remove from array
  116. * @return array with identical contents excluding values of `value`
  117. */ // TODO
  118. public static String[] removeValue(String[] array, String valueToRemove) {
  119. //use method from above
  120. int index = 0;
  121. int count = getNumberOfOccurrences(array,valueToRemove);
  122. String[] arr2 = new String[array.length -count];
  123. for(int i= 0; i<array.length;i++){
  124. if(array[i] != valueToRemove){
  125. arr2[index]=array[i];
  126. index++;
  127. }
  128. }
  129. return arr2;
  130. }
  131. /**
  132. * @param array array of chars
  133. * @return array of Strings with consecutive duplicates removes
  134. */ // TODO
  135. public static String[] removeConsecutiveDuplicates(String[] array) {
  136. String removeString = array[0] + " ";
  137. for(int i =1; i<array.length; i++){
  138. if(!array[i].equals(array[i-1])){
  139. removeString+=array[i] + " ";
  140. }
  141. }
  142. String [] removeStringA = removeString.split(" ");
  143. return removeStringA;
  144. }
  145. /**
  146. * @param array array of chars
  147. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  148. */ // TODO
  149. public static String[] packConsecutiveDuplicates(String[] array) {
  150. String removeString = array[0];
  151. for(int i =1; i<array.length; i++){
  152. if(array[i].equals(array[i-1])){
  153. removeString+=array[i];
  154. }
  155. else if(!array[i].equals(array[i-1])){
  156. removeString += " " + array[i];
  157. }
  158. }
  159. String[] removeStringA= removeString.split(" ");
  160. return removeStringA;
  161. }
  162. }