Advertisement
AngelGiurov

1

Jul 19th, 2021
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. package com.telerikacademy.core.arrays;
  2.  
  3. @SuppressWarnings({"ManualArrayCopy", "ExplicitArrayFilling"})
  4. public class ArrayHelpers {
  5.  
  6. public static int[] add(int[] source, int element) {
  7.  
  8. int[] add = new int[source.length + 1];
  9. for (int i = 0; i < add.length; i++) {
  10. if (i == add.length - 1) {
  11. add[i] = element;
  12. break;
  13. }
  14. add[i] = source[i];
  15. }
  16. return add; // Acho
  17. }
  18.  
  19. /**
  20. * Create a new list with the same elements as the source list and
  21. * add the new element in the beginning.
  22. *
  23. * @param source The original list.
  24. * @param element The first element in the new list.
  25. * @return A new list with added first element.
  26. * @author Galin Todorov
  27. */
  28. public static int[] addFirst(int[] source, int element) {
  29. int[] array = new int[source.length + 1];
  30. for (int i = 1; i < array.length; i++) {
  31. array[i] = source[i - 1];
  32. }
  33. array[0] = element;
  34.  
  35. return array;
  36. }
  37.  
  38. public static int[] addAll(int[] source, int... elements) {
  39. // Koko
  40. return new int[1];
  41. }
  42.  
  43. /**
  44. * Checking if that element is in the array.
  45. *
  46. * @param source Get array.
  47. * @param element Get element.
  48. * @return boolean
  49. * @author Pavel Milanov
  50. */
  51. public static boolean contains(int[] source, int element) {
  52. for (int i = 0; i < source.length; i++) {
  53. if (source[i] == element) {
  54. return true;
  55. }
  56. }
  57. return false;
  58. }
  59.  
  60. /**
  61. * Copies count elements from sourceArray into destinationArray
  62. *
  63. * @param sourceArray int[] - The array to copy from
  64. * @param destinationArray int[] - The array to copy to
  65. * @param count int - The number of elements to copy
  66. * @return A string that represents the concatenation of string1 followed by string2's characters.
  67. * @author Teodora Georgieva
  68. */
  69. public static void copy(int[] sourceArray, int[] destinationArray, int count) {
  70. if (destinationArray.length >= sourceArray.length) {
  71. for (int i = 0; i < sourceArray.length; i++) {
  72. destinationArray[i] = sourceArray[i];
  73. }
  74. } else {
  75. for (int i = 0; i < count; i++) {
  76. destinationArray[i] = sourceArray[i];
  77. }
  78. }
  79. }
  80.  
  81. public static void copyFrom(int[] sourceArray, int sourceStartIndex,
  82. int[] destinationArray, int destStartIndex, int count) {
  83.  
  84. for (int i = 0; i < count; i++) {
  85. destinationArray[destStartIndex] = sourceArray[sourceStartIndex];
  86. destStartIndex++;
  87. sourceStartIndex++;
  88. }
  89. //Acho
  90. }
  91.  
  92. /**
  93. * Fills all indexes of an integer array with an integer.
  94. *
  95. * @param source An integer array.
  96. * @param element An integer that fills the array.
  97. * @author Galin Todorov
  98. */
  99. public static void fill(int[] source, int element) {
  100. for (int i = 0; i < source.length; i++) {
  101. source[i] = element;
  102. }
  103. }
  104.  
  105. public static int firstIndexOf(int[] source, int target) {
  106. // Koko
  107. return 0;
  108. }
  109.  
  110. /**
  111. * This method implement an element in source with given index to place it.
  112. *
  113. * @param source Get an integer array.
  114. * @param index Get an index to place it.
  115. * @param element Get an element to place.
  116. * @return array
  117. * @author Pavel Milanov
  118. */
  119. public static int[] insert(int[] source, int index, int element) {
  120. int[] array = new int[source.length + 1];
  121.  
  122. for (int i = 0; i < array.length; i++) {
  123. if (i < index) {
  124. array[i] = source[i];
  125. }
  126. if (i == index) {
  127. array[i] = element;
  128. }
  129. if (i > index) {
  130. array[i] = source[i - 1];
  131. }
  132. }
  133. return array;
  134. }
  135.  
  136. /**
  137. * Checks if index is a valid index in source.
  138. *
  139. * @param source int[] - The array to check against
  140. * @param index int - The index to check for
  141. * @return boolean - true if index is valid, otherwise, false
  142. * @author Teodora Georgieva
  143. */
  144. public static boolean isValidIndex(int[] source, int index) {
  145. for (int i = 0; i < source.length; i++) {
  146. if (i == index) {
  147. return true;
  148. }
  149. }
  150. return false;
  151. }
  152.  
  153. public static int lastIndexOf(int[] source, int target) {
  154. int index = -1;
  155. for (int i = 0; i < source.length; i++) {
  156. if (target == source[i]) {
  157. index = i;
  158. }
  159. }
  160. return index;
  161. //
  162. }
  163.  
  164. /**
  165. * Removes all occurrences of element within source
  166. * @param source int[] - The array to remove from
  167. * @param element int - The element to check for
  168. * @return int[] - A new array with all occurrences of element removed
  169. *
  170. * @author Galin Todorov
  171. */
  172. public static int[] removeAllOccurrences(int[] source, int element) {
  173. int count = 0;
  174. for (int i : source) {
  175. if (i == element) {
  176. count++;
  177. }
  178. }
  179. return source;
  180. }
  181.  
  182. public static void reverse(int[] arrayToReverse) {
  183. //Ira
  184.  
  185. }
  186.  
  187. public static int[] section(int[] source, int startIndex, int endIndex) {
  188. //Ira
  189. return new int[1];
  190. }
  191.  
  192. }
  193.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement