StringArrayUtils.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. char[] alphabet = new char[26];
  75. for(String str: array){
  76. char[] chars = str.toCharArray();
  77. }
  78. return false;
  79. }
  80. private void isLetter(){
  81. }
  82. private void isInCharArray(char[] chars, char Char){
  83. }
  84. /**
  85. * @param array array of String objects
  86. * @param value value to check array for
  87. * @return number of occurrences the specified `value` has occurred
  88. */ // TODO
  89. public static int getNumberOfOccurrences(String[] array, String value) {
  90. int counter = 0;
  91. for(String str: array){
  92. if(str.equals(value)) {
  93. counter++;
  94. }
  95. }
  96. return counter;
  97. }
  98. /**
  99. * @param array array of String objects
  100. * @param valueToRemove value to remove from array
  101. * @return array with identical contents excluding values of `value`
  102. */ // TODO
  103. public static String[] removeValue(String[] array, String valueToRemove) {
  104. //ArrayList<String> strings = new ArrayList<String>(Arrays.asList(array));
  105. //for(int i = 0; i < string.size()
  106. return null;
  107. }
  108. /**
  109. * @param array array of chars
  110. * @return array of Strings with consecutive duplicates removes
  111. */ // TODO
  112. public static String[] removeConsecutiveDuplicates(String[] array) {
  113. return null;
  114. }
  115. /**
  116. * @param array array of chars
  117. * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
  118. */ // TODO
  119. public static String[] packConsecutiveDuplicates(String[] array) {
  120. return null;
  121. }
  122. }