StringArrayUtils.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * Created by leon on 1/29/18.
  3. */
  4. public class StringArrayUtils {
  5. /**
  6. * @param array array of String objects
  7. * @return first element of specified array
  8. */ // TODO
  9. public static String getFirstElement(String[] array) {
  10. return array[0];
  11. }
  12. /**
  13. * @param array array of String objects
  14. * @return second element in specified array
  15. */
  16. public static String getSecondElement(String[] array) {
  17. return array[1];
  18. }
  19. /**
  20. * @param array array of String objects
  21. * @return last element in specified array
  22. */ // TODO
  23. public static String getLastElement(String[] array) {
  24. //String[] array = {"brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  25. String word = "";
  26. for(int i = 0; i < array.length -1; i++){
  27. return word = array[array.length-1];
  28. }
  29. return word;
  30. }
  31. /**
  32. * @param array array of String objects
  33. * @return second to last element in specified array
  34. */ // TODO
  35. public static String getSecondToLastElement(String[] array) {
  36. String word = "";
  37. for(int i = 0; i < array.length -2; i++){
  38. return word = array[array.length-2];
  39. }
  40. return word;
  41. }
  42. /**
  43. * @param array array of String objects
  44. * @param value value to check array for
  45. * @return true if the array contains the specified `value`
  46. */ // TODO
  47. public static boolean contains(String[] array, String value) {
  48. boolean hold = false;
  49. for(String s:array){
  50. if(s.equals(value)){
  51. return hold = true;
  52. }
  53. }
  54. return hold;
  55. }
  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[] tempStringArray = new String[array.length];
  62. System.arraycopy( array, 0, tempStringArray, 0, array.length );
  63. for(int i = 0 ;i<tempStringArray.length/2;i++){
  64. String temparray = tempStringArray[i];
  65. tempStringArray[i] = tempStringArray[tempStringArray.length -1 - i];
  66. tempStringArray[tempStringArray.length - 1 - i] = temparray;
  67. }
  68. return tempStringArray;
  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. */ // TODO
  74. public static boolean isPalindromic(String[] array) {
  75. /*
  76. * I'm trying to reverse the order of the array
  77. * than compare if array is equals to array
  78. */
  79. boolean returnPal = false;
  80. String [] reverseArray = reverse(array);
  81. /*
  82. *
  83. */
  84. for(int i = 0; i < array.length; i++){
  85. System.out.println("array[i] " + array[i] + " reverseArray[i] " + reverseArray[i]);
  86. if(array[i].equals(reverseArray[i])){
  87. returnPal = true;
  88. }
  89. }
  90. return returnPal;
  91. }
  92. /**
  93. * @param array array of String objects
  94. * @return true if each letter in the alphabet has been used in the array
  95. */ // TODO
  96. public static boolean isPangramic(String[] array) {
  97. /*int letters = 26;
  98. * if(isPangramic.length() < letters){
  99. * }
  100. */
  101. int letters = 26;
  102. boolean pangram = false;
  103. if(array.length < letters){
  104. pangram = true;
  105. }
  106. if(array.length == letters){
  107. pangram = false;
  108. }else{
  109. pangram = true;
  110. }
  111. return pangram;
  112. }
  113. /**
  114. * @param array array of String objects
  115. * @param value value to check array for
  116. * @return number of occurrences the specified `value` has occurred
  117. */ // TODO
  118. public static int getNumberOfOccurrences(String[] array, String value) {
  119. /*
  120. *
  121. */
  122. int count = 0;
  123. for(int i = 0;i < array.length;i++){
  124. if(array[i] == value){
  125. count++;
  126. }
  127. }
  128. return count;
  129. }
  130. /**
  131. * @param array array of String objects
  132. * @param valueToRemove value to remove from array
  133. * @return array with identical contents excluding values of `value`
  134. */ // TODO
  135. public static String[] removeValue(String[] array, String valueToRemove) {
  136. return null;
  137. }
  138. /**
  139. * @param array array of chars
  140. * @return array of Strings with consecutive duplicates removes
  141. */ // TODO
  142. public static String[] removeConsecutiveDuplicates(String[] array) {
  143. return null;
  144. }
  145. /**
  146. * @param array array of chars
  147. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  148. */ // TODO
  149. public static String[] packConsecutiveDuplicates(String[] array) {
  150. return null;
  151. }
  152. }