StringArrayUtils.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Collections;
  4. import java.util.List;
  5. import java.util.*;
  6. /**
  7. * Created by leon on 1/29/18.
  8. */
  9. public class StringArrayUtils {
  10. /**
  11. * @param array array of String objects
  12. * @return first element of specified array
  13. */ // DONE
  14. public static String getFirstElement(String[] array) {
  15. return array[0];
  16. }
  17. /**
  18. * @param array array of String objects
  19. * @return second element in specified array
  20. * DONE
  21. */
  22. public static String getSecondElement(String[] array) {
  23. return array[1];
  24. }
  25. /**
  26. * @param array array of String objects
  27. * @return last element in specified array
  28. */ // DONE
  29. public static String getLastElement(String[] array) {
  30. return array[array.length-1];
  31. }
  32. /**
  33. * @param array array of String objects
  34. * @return second to last element in specified array
  35. */ // DONE
  36. public static String getSecondToLastElement(String[] array) {
  37. return array[array.length-2];
  38. }
  39. /**
  40. * @param array array of String objects
  41. * @param value value to check array for
  42. * @return true if the array contains the specified `value`
  43. */ // DONE
  44. public static boolean contains(String[] array, String value) {
  45. boolean check = false;
  46. for (int i = 0; i<array.length; i++){
  47. if (array[i] == value){
  48. check = true;}
  49. }
  50. return check;
  51. }
  52. /**
  53. * @param array of String objects
  54. * @return an array with identical contents in reverse order
  55. */ // DONE
  56. public static String[] reverse(String[] array) {
  57. for (int i = 0; i<array.length/2; i++){
  58. String temp = array[i];
  59. array[i] = array[array.length-1-i];
  60. array[array.length-1-i] = temp;
  61. }
  62. return array;
  63. }
  64. /**
  65. * @param array array of String objects
  66. * @return true if the order of the array is the same backwards and forwards
  67. */ // DONE
  68. public static boolean isPalindromic(String[] array) {
  69. boolean check = false;
  70. for (int i=0; i<array.length/2; i++){
  71. String temp = array[i];
  72. array[i] = array[array.length-1-i];
  73. array[array.length-1-i] = temp;
  74. if ( temp == array[i]){
  75. check = true;
  76. }
  77. }
  78. return check;
  79. }
  80. /**
  81. * @param array array of String objects
  82. * @return true if each letter in the alphabet has been used in the array
  83. */ // TODO
  84. public static boolean isPangramic(String[] array) {
  85. boolean check = false;
  86. //Scanner reader = new Scanner();
  87. String string = new String();//reader.nextLine();
  88. Set character = new HashSet();
  89. Set set = new HashSet();
  90. for (char c:string.toUpperCase().toCharArray()){
  91. if (character.equals(c)){
  92. set.add(c);}
  93. }
  94. if (set.size() == 26){
  95. check = true;}
  96. else {
  97. check = false;}
  98. return check;
  99. }
  100. /**
  101. * @param array array of String objects
  102. * @param value value to check array for
  103. * @return number of occurrences the specified `value` has occurred
  104. */ // Done
  105. public static int getNumberOfOccurrences(String[] array, String value) {
  106. int counter = 0;
  107. for (int i = 0; i<array.length; i++){
  108. if (array[i] == array[i+1]){
  109. counter=counter+1;}
  110. }
  111. return counter;
  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. */ // Done
  118. public static String[] removeValue(String[] array, String valueToRemove) {
  119. //sort array, create new array, compare i, add new i to new array.
  120. Arrays.sort(array);
  121. ArrayList fresh = new ArrayList();
  122. for (int i=0;i<array.length;i++){
  123. if (array[i] != valueToRemove){
  124. fresh.add(array[i]);
  125. }
  126. }
  127. String[] list = (String[])(fresh.toArray()); //needed to convert to a string array, not a string
  128. return list;
  129. //String delete = value.replaceAll (Character.toString(charToRemove), "");
  130. //return delete;
  131. //return list.toString();
  132. }
  133. /**
  134. * @param array array of chars
  135. * @return array of Strings with consecutive duplicates removes
  136. */ // TODO
  137. public static String[] removeConsecutiveDuplicates(String[] array) {
  138. Arrays.sort(array);
  139. ArrayList fresh = new ArrayList();
  140. for (int i=0; i<array.length; i++){
  141. if (array[i] != array[i-1] && array[i] != array[i+1]) {
  142. fresh.add(array[i]);
  143. }
  144. }
  145. String[] list = (String[])(fresh.toArray());
  146. return list;
  147. }
  148. /**
  149. * @param array array of chars
  150. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  151. */ // TODO
  152. public static String[] packConsecutiveDuplicates(String[] array) {
  153. return null;
  154. }
  155. }