StringArrayUtils.java# 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package treeSet
  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. for(String element:array){
  42. if(element==value){
  43. return true;
  44. }
  45. }
  46. return false;
  47. }
  48. /**
  49. * @param array of String objects
  50. * @return an array with identical contents in reverse order
  51. */ // TODO
  52. public static String[] reverse(String[] array) {
  53. String flip="";
  54. for(int i=0;i<array.length/2;i++){
  55. flip=array[i];
  56. array[i]=array[array.length-1-i];
  57. array[array.length-1-i] = flip;
  58. }
  59. return array;
  60. }
  61. /**
  62. * @param array array of String objects
  63. * @return true if the order of the array is the same backwards and forwards
  64. */ // TODO
  65. public static boolean isPalindromic(String[] array) {
  66. int palindrome = array.length;
  67. for(int i=0;i<array.length;i++){
  68. String start = array[i];
  69. String end =array[--palindrome];
  70. if(palindrome<i){
  71. return true;
  72. }
  73. if(start!=end){
  74. return false;
  75. }
  76. }
  77. return true;
  78. }
  79. /**
  80. * @param array array of String objects
  81. * @return true if each letter in the alphabet has been used in the array
  82. */ // TODO
  83. public static boolean isPangramic(String[] array) {
  84. int counter=0;
  85. int i;
  86. /*for(String e:array){
  87. if(e==(i = 'a';i <= 'z'));{
  88. counter++;}}
  89. if(counter<26){
  90. return false;
  91. }return true;*/
  92. }
  93. /**
  94. * @param array array of String objects
  95. * @param value value to check array for
  96. * @return number of occurrences the specified `value` has occurred
  97. */ // TODO
  98. public static int getNumberOfOccurrences(String[] array, String value) {
  99. int counter=0;
  100. for(int i=0;i<array.length;i++){
  101. if(array[i]==value){
  102. counter++;}
  103. }
  104. return counter;
  105. }
  106. /**
  107. * @param array array of String objects
  108. * @param valueToRemove value to remove from array
  109. * @return array with identical contents excluding values of `value`
  110. */ // TODO
  111. public static String[] removeValue(String[] array, String valueToRemove) {
  112. ArrayList<String> remove = new ArrayList<String>();
  113. for(String e : array){
  114. if(!e.equals(valueToRemove)){
  115. remove.add(e);
  116. }
  117. }
  118. return remove.toArray(new String[remove.size()]);
  119. }
  120. /**
  121. * @param array array of chars
  122. * @return array of Strings with consecutive duplicates removes
  123. */ // TODO
  124. public static String[] removeConsecutiveDuplicates(String[] array) {
  125. SortedSet<String> remove = new TreeSet<>(array);
  126. for(String e:remove){
  127. if(e==e){
  128. remove.remove(e);
  129. }
  130. }
  131. return array;
  132. }
  133. /**
  134. * @param array array of chars
  135. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  136. */ // TODO
  137. public static String[] packConsecutiveDuplicates(String[] array) {
  138. return null;
  139. }
  140. }