StringArrayUtils.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import java.lang.String;
  2. /**
  3. * Created by leon on 1/29/18.
  4. */
  5. import java.util.*;
  6. import java.io.*;
  7. import java.util.Arrays;
  8. import java.util.Scanner;
  9. import java.util.List;
  10. import java.util.ArrayList;
  11. public class StringArrayUtils {
  12. /**
  13. * @param array array of String objects
  14. * @return first element of specified array
  15. */ // TODO
  16. public static String getFirstElement(String[] array) {
  17. String first = array[0];
  18. return first;
  19. }
  20. /**
  21. * @param array array of String objects
  22. * @return second element in specified array
  23. */
  24. public static String getSecondElement(String[] array) {
  25. String second = array[1];
  26. return second;
  27. }
  28. /**
  29. * @param array array of String objects
  30. * @return last element in specified array
  31. */ // TODO
  32. public static String getLastElement(String[] array) {
  33. String last = array[array.length-1];
  34. return last;
  35. }
  36. /**
  37. * @param array array of String objects
  38. * @return second to last element in specified array
  39. */ // TODO
  40. public static String getSecondToLastElement(String[] array) {
  41. String secondLast = array[array.length-2];
  42. return secondLast;
  43. }
  44. /**
  45. * @param array array of String objects
  46. * @param value value to check array for
  47. * @return true if the array contains the specified `value`
  48. */ // TODO
  49. public static boolean contains(String[] array, String value) {
  50. for(int i =0; i< array.length;i++)
  51. {
  52. if(array[i].equals(value))
  53. {return true;}
  54. }
  55. return false;
  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[] temp = new String[array.length];
  62. int j =0;
  63. for(int i =array.length-1;i >= 0;i--)
  64. {
  65. temp[j] = array[i];
  66. j++;
  67. }
  68. return temp;
  69. }
  70. /**
  71. * @param array array of String objects
  72. * @return true if the order of the array is the same backwards and forwards
  73. */
  74. public static boolean isPalindromic(String[] array) {
  75. String[] reverseArr = new String[array.length];
  76. reverseArr = reverse(array);
  77. for (int i=0;i < reverseArr.length;i++){
  78. if(!array[i].equalsIgnoreCase(reverseArr[i]))
  79. {
  80. return false;
  81. }
  82. }
  83. return true;
  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. String alph = "abcdefghijklmnopqrstuvwxyz";
  91. StringBuilder build = new StringBuilder();
  92. int count =0;
  93. for(int i =0;i < array.length;i++)
  94. {
  95. build.append(array[i]);
  96. }
  97. String newStr = build.toString();
  98. for(int j =0; j < alph.length();j++)
  99. {
  100. int index = newStr.indexOf(alph.charAt(j));
  101. if( index == -1)
  102. {
  103. break;
  104. }
  105. count = 1;
  106. }
  107. if(count ==1)
  108. return true;
  109. else
  110. return false;
  111. }
  112. /**
  113. * @param array array of String objects
  114. * @param value value to check array for
  115. * @return number of occurrences the specified `value` has occurred
  116. */ // TODO
  117. public static int getNumberOfOccurrences(String[] array, String value) {
  118. int count = 0;
  119. for(int i=0;i < array.length;i++)
  120. {
  121. if(array[i].equalsIgnoreCase(value))
  122. {
  123. count++;
  124. }
  125. }
  126. return count;
  127. }
  128. /**
  129. * @param array array of String objects
  130. * @param valueToRemove value to remove from array
  131. * @return array with identical contents excluding values of `value`
  132. */ // TODO
  133. public static String[] removeValue(String[] array, String valueToRemove) {
  134. ArrayList <String >arrList = new ArrayList<String>();
  135. int j =0;
  136. int count =0;
  137. for(int i =0;i < array.length;i++)
  138. {
  139. if(!(array[i]).equals(valueToRemove))
  140. {
  141. arrList.add(array[i]);
  142. }
  143. }
  144. String[] arrayNew = arrList.toArray(new String[0]);
  145. return arrayNew;
  146. }
  147. /**
  148. * @param array array of chars
  149. * @return array of Strings with consecutive duplicates removes
  150. */ // TODO*/
  151. public static String[] removeConsecutiveDuplicates(String[] array) {
  152. int k = 0;
  153. ArrayList<String> resultList = new ArrayList<String>();
  154. //resultList.add(array[0]);
  155. for(int i =0;i < array.length;i++)
  156. {
  157. if(!array[i].equals(array[i+1]))
  158. {
  159. resultList.add(array[i]);
  160. }
  161. }
  162. String[] newArray = resultList.toArray(new String[0]);
  163. return newArray;
  164. }
  165. /**
  166. * @param array array of chars
  167. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  168. */
  169. public static String[] packConsecutiveDuplicates(String[] array) {
  170. ArrayList<String> list = new ArrayList<String>();
  171. //list.add(array[0]);
  172. //int k =0;
  173. StringBuilder build = new StringBuilder();
  174. for(int i =0;i <array.length;i++)
  175. {
  176. if(array[i].equalsIgnoreCase(array[i+1]))
  177. {
  178. build.append(array[i]);
  179. String str = build.toString();
  180. list.add(str);
  181. build = new StringBuilder();
  182. }
  183. else
  184. {
  185. build.append(array[i]);
  186. }
  187. }
  188. String[] newArray = list.toArray(new String[0]);
  189. return newArray;
  190. }
  191. }