StringArrayUtils.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /**
  2. * Created by leon on 1/29/18.
  3. */
  4. import java.util.*;
  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. int last_index=array.length-1;
  26. return array[last_index];
  27. }
  28. /**
  29. * @param array array of String objects
  30. * @return second to last element in specified array
  31. */ // TODO
  32. public static String getSecondToLastElement(String[] array) {
  33. int last_index=array.length-2;
  34. return array[last_index];
  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 b=false;
  43. for(int i=0;i<array.length;i++)
  44. {
  45. if(array[i].equals(value))
  46. {
  47. b=true;
  48. }
  49. }
  50. return b;
  51. }
  52. /**
  53. * @param array of String objects
  54. * @return an array with identical contents in reverse order
  55. */ // TODO
  56. public static String[] reverse(String[] array) {
  57. String[] str=new String[array.length];
  58. int k=0;
  59. for(int i=array.length-1;i>=0;i--)
  60. {
  61. str[k]=array[i];
  62. k++;
  63. }
  64. //System.out.println((str));
  65. return str;
  66. }
  67. /**
  68. * @param array array of String objects
  69. * @return true if the order of the array is the same backwards and forwards
  70. */ // TODO
  71. public static boolean isPalindromic(String[] array) {
  72. String[] reversed=new String[array.length];
  73. reversed=reverse(array);
  74. boolean flag=true;
  75. for( int i=0;i<reversed.length;i++)
  76. {
  77. if(!array[i].equals(reversed[i]))
  78. {
  79. flag=false;
  80. break;
  81. }
  82. }
  83. return flag;
  84. }
  85. /**
  86. * @param array array of String objects
  87. * @return true if each letter in the alphabet has been used in the array
  88. */ // TODO
  89. public static boolean isPangramic(String[] array) {
  90. ArrayList<Character> albha = new ArrayList<Character>();
  91. String str="";
  92. int count =0;
  93. boolean result=false;
  94. for(int i=0;i<array.length;i++)
  95. {
  96. str=str+array[i];
  97. }
  98. System.out.println(str);
  99. str=str.toLowerCase();
  100. for(int i=0;i<str.length();i++)
  101. {
  102. int k=str.charAt(i);
  103. if(k>=65&&k<=90 || k>=97&&k<=122)
  104. {
  105. if(!albha.contains(str.charAt(i)))
  106. {
  107. albha.add(str.charAt(i));
  108. count ++;
  109. }
  110. }
  111. }
  112. System.out.println(count);
  113. System.out.println(albha);
  114. if(count==26)
  115. {
  116. result=true;
  117. }
  118. else
  119. {
  120. result=false;
  121. }
  122. return result;
  123. }
  124. /**
  125. * @param array array of String objects
  126. * @param value value to check array for
  127. * @return number of occurrences the specified `value` has occurred
  128. */ // TODO
  129. public static int getNumberOfOccurrences(String[] array, String value) {
  130. int k=0;
  131. for(int i=0;i<array.length;i++)
  132. {
  133. if(value.equals(array[i].toString()))
  134. {
  135. k++;
  136. }
  137. }
  138. return k;
  139. }
  140. /**
  141. * @param array array of String objects
  142. * @param valueToRemove value to remove from array
  143. * @return array with identical contents excluding values of `value`
  144. */ // TODO
  145. public static String[] removeValue(String[] array, String valueToRemove) {
  146. int k=0;
  147. int n=getNumberOfOccurrences(array,valueToRemove);
  148. String[] newArray= new String[array.length-n];
  149. for(int i=0;i<array.length;i++)
  150. {
  151. if(!array[i].equals(valueToRemove))
  152. {
  153. newArray[k]=array[i];
  154. k++;
  155. }
  156. }
  157. System.out.println(Arrays.toString(array));
  158. return newArray;
  159. }
  160. /**
  161. * @param array array of chars
  162. * @return array of Strings with consecutive duplicates removes
  163. */ // TODO
  164. public static String[] removeConsecutiveDuplicates(String[] array) {
  165. int k=0;
  166. ArrayList<String> resultList = new ArrayList<String>();
  167. resultList.add(array[0]);
  168. for(int i = 1; i < array.length; i++) {
  169. if(!array[i-1].equals(array[i])) {
  170. resultList.add(array[i]);
  171. }
  172. }
  173. String[] resultArray = resultList.toArray(new String[resultList.size()]);
  174. return resultArray;
  175. }
  176. /**
  177. * @param array array of chars
  178. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  179. */ // TODO
  180. public static String[] packConsecutiveDuplicates(String[] array) {
  181. ArrayList<String> list = new ArrayList<>();
  182. list.add(array[0]);
  183. int k=0;
  184. for(int i = 1 ; i < array.length ; i++) {
  185. if(array[i-1].equalsIgnoreCase(array[i])){
  186. String lastElement = list.get(list.size()-1);
  187. lastElement = lastElement.concat(array[i]);
  188. list.set(list.size()-1,lastElement);
  189. }
  190. else{
  191. list.add(array[i]);
  192. }
  193. }
  194. String[] newArray = list.toArray(new String[list.size()]);
  195. return newArray;
  196. }
  197. }