Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.telerikacademy.core.arrays;
- @SuppressWarnings({"ManualArrayCopy", "ExplicitArrayFilling"})
- public class ArrayHelpers {
- public static int[] add(int[] source, int element) {
- int[] add = new int[source.length + 1];
- for (int i = 0; i < add.length; i++) {
- if (i == add.length - 1) {
- add[i] = element;
- break;
- }
- add[i] = source[i];
- }
- return add; // Acho
- }
- /**
- * Create a new list with the same elements as the source list and
- * add the new element in the beginning.
- *
- * @param source The original list.
- * @param element The first element in the new list.
- * @return A new list with added first element.
- * @author Galin Todorov
- */
- public static int[] addFirst(int[] source, int element) {
- int[] array = new int[source.length + 1];
- for (int i = 1; i < array.length; i++) {
- array[i] = source[i - 1];
- }
- array[0] = element;
- return array;
- }
- public static int[] addAll(int[] source, int... elements) {
- // Koko
- return new int[1];
- }
- /**
- * Checking if that element is in the array.
- *
- * @param source Get array.
- * @param element Get element.
- * @return boolean
- * @author Pavel Milanov
- */
- public static boolean contains(int[] source, int element) {
- for (int i = 0; i < source.length; i++) {
- if (source[i] == element) {
- return true;
- }
- }
- return false;
- }
- /**
- * Copies count elements from sourceArray into destinationArray
- *
- * @param sourceArray int[] - The array to copy from
- * @param destinationArray int[] - The array to copy to
- * @param count int - The number of elements to copy
- * @return A string that represents the concatenation of string1 followed by string2's characters.
- * @author Teodora Georgieva
- */
- public static void copy(int[] sourceArray, int[] destinationArray, int count) {
- if (destinationArray.length >= sourceArray.length) {
- for (int i = 0; i < sourceArray.length; i++) {
- destinationArray[i] = sourceArray[i];
- }
- } else {
- for (int i = 0; i < count; i++) {
- destinationArray[i] = sourceArray[i];
- }
- }
- }
- public static void copyFrom(int[] sourceArray, int sourceStartIndex,
- int[] destinationArray, int destStartIndex, int count) {
- for (int i = 0; i < count; i++) {
- destinationArray[destStartIndex] = sourceArray[sourceStartIndex];
- destStartIndex++;
- sourceStartIndex++;
- }
- //Acho
- }
- /**
- * Fills all indexes of an integer array with an integer.
- *
- * @param source An integer array.
- * @param element An integer that fills the array.
- * @author Galin Todorov
- */
- public static void fill(int[] source, int element) {
- for (int i = 0; i < source.length; i++) {
- source[i] = element;
- }
- }
- public static int firstIndexOf(int[] source, int target) {
- // Koko
- return 0;
- }
- /**
- * This method implement an element in source with given index to place it.
- *
- * @param source Get an integer array.
- * @param index Get an index to place it.
- * @param element Get an element to place.
- * @return array
- * @author Pavel Milanov
- */
- public static int[] insert(int[] source, int index, int element) {
- int[] array = new int[source.length + 1];
- for (int i = 0; i < array.length; i++) {
- if (i < index) {
- array[i] = source[i];
- }
- if (i == index) {
- array[i] = element;
- }
- if (i > index) {
- array[i] = source[i - 1];
- }
- }
- return array;
- }
- /**
- * Checks if index is a valid index in source.
- *
- * @param source int[] - The array to check against
- * @param index int - The index to check for
- * @return boolean - true if index is valid, otherwise, false
- * @author Teodora Georgieva
- */
- public static boolean isValidIndex(int[] source, int index) {
- for (int i = 0; i < source.length; i++) {
- if (i == index) {
- return true;
- }
- }
- return false;
- }
- public static int lastIndexOf(int[] source, int target) {
- int index = -1;
- for (int i = 0; i < source.length; i++) {
- if (target == source[i]) {
- index = i;
- }
- }
- return index;
- //
- }
- /**
- * Removes all occurrences of element within source
- * @param source int[] - The array to remove from
- * @param element int - The element to check for
- * @return int[] - A new array with all occurrences of element removed
- *
- * @author Galin Todorov
- */
- public static int[] removeAllOccurrences(int[] source, int element) {
- int count = 0;
- for (int i : source) {
- if (i == element) {
- count++;
- }
- }
- return source;
- }
- public static void reverse(int[] arrayToReverse) {
- //Ira
- }
- public static int[] section(int[] source, int startIndex, int endIndex) {
- //Ira
- return new int[1];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement