StringArrayUtils.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import java.util.*;
  2. import java.util.ArrayList;
  3. /**
  4. * Created by leon on 1/29/18.
  5. */
  6. public class StringArrayUtils {
  7. /**
  8. * @param array array of String objects
  9. * @return first element of specified array
  10. */ // TODO
  11. public static String getFirstElement(String[] array) {
  12. return array[0];
  13. }
  14. /**
  15. * @param array array of String objects
  16. * @return second element in specified array
  17. */
  18. public static String getSecondElement(String[] array) {
  19. return array[1];
  20. }
  21. /**
  22. * @param array array of String objects
  23. * @return last element in specified array
  24. */ // TODO
  25. public static String getLastElement(String[] array) {
  26. return array[array.length - 1];
  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. return array[array.length - 2];
  34. }
  35. /**
  36. * @param array array of String objects
  37. * @param value value to check array for
  38. * @return true if the array contains the specified `value`
  39. */ // TODO
  40. public static boolean contains(String[] array, String value) {
  41. return Arrays.asList(array).contains(value);
  42. }
  43. /**
  44. * @param array of String objects
  45. * @return an array with identical contents in reverse order
  46. */ // TODO
  47. public static String[] reverse(String[] array) {
  48. for (int i = 0; i < array.length/2; i++){
  49. String temp = array[i];
  50. array[i] = array[(array.length -1) - i];
  51. array[(array.length -1) - i] = temp;
  52. }
  53. return array;
  54. }
  55. /**
  56. * @param array array of String objects
  57. * @return true if the order of the array is the same backwards and forwards
  58. */ // TODO
  59. public static boolean isPalindromic(String[] array) {
  60. boolean isPalindromic = true;
  61. for(int i = 0; i < array.length/2 ; i++){
  62. if(array[i] != array[( array.length - 1 ) - i]){
  63. isPalindromic = false;
  64. break;
  65. };
  66. }
  67. return isPalindromic;
  68. }
  69. /**
  70. * @param array array of String objects
  71. * @return true if each letter in the alphabet has been used in the array
  72. */ // TODO
  73. public static boolean isPangramic(String[] array) {
  74. ArrayList<Character> Chars = new ArrayList<Character>();
  75. for(String str: array){
  76. char[] chars = str.toCharArray();
  77. for(char car : chars){
  78. if(!Chars.contains(car)){
  79. Chars.add(car);
  80. }
  81. }
  82. }
  83. return Chars.size() == 26 ? true : false;
  84. }
  85. /**
  86. * @param array array of String objects
  87. * @param value value to check array for
  88. * @return number of occurrences the specified `value` has occurred
  89. */ // TODO
  90. public static int getNumberOfOccurrences(String[] array, String value) {
  91. int counter = 0;
  92. for(String str: array){
  93. if(str.equals(value)) {
  94. counter++;
  95. }
  96. }
  97. return counter;
  98. }
  99. /**
  100. * @param array array of String objects
  101. * @param valueToRemove value to remove from array
  102. * @return array with identical contents excluding values of `value`
  103. */ // TODO
  104. public static String[] removeValue(String[] array, String valueToRemove) {
  105. ArrayList<String> strings = new ArrayList<String>();
  106. for(String str : array){
  107. if(!str.equals(valueToRemove)){
  108. strings.add(str);
  109. }
  110. }
  111. return strings.toArray(new String[strings.size()]);
  112. }
  113. /**
  114. * @param array array of chars
  115. * @return array of Strings with consecutive duplicates removes
  116. */ // TODO
  117. public static String[] removeConsecutiveDuplicates(String[] array) {
  118. ArrayList<String> strings = new ArrayList<String>();
  119. String prevString = "";
  120. for(String str: array){
  121. if(!prevString.equals(str)){
  122. strings.add(str);
  123. }
  124. prevString = str;
  125. }
  126. return strings.toArray(new String[strings.size()]);
  127. }
  128. /**
  129. * @param array array of chars
  130. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  131. */ // TODO
  132. public static String[] packConsecutiveDuplicates(String[] array) {
  133. ArrayList<String> strings = new ArrayList<String>();
  134. boolean isDuplicated;
  135. for(String str1: array){
  136. isDuplicated = false;
  137. for(String str2: array){
  138. if(str1 == str2){
  139. isDuplicated = true;
  140. }
  141. }
  142. if(!isDuplicated){
  143. strings.add(str1);
  144. }
  145. }
  146. return strings.toArray(new String[strings.size()]);
  147. }
  148. }