All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.invation.code.toval.misc.ArrayUtils Maven / Gradle / Ivy

Go to download

TOVAL comprises a set of java classes for common programming issues. It includes utils for arrays, lists, sets and collections for convenient handling and modification, but also support for mathematic definitions concerning logic (clauses + resolution) together with some algorithms for permutations, powersets and resolution. Additionally it contains a number of types for multisets, matrices with object keys and much more.

The newest version!
package de.invation.code.toval.misc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Formatter;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import de.invation.code.toval.math.Permutations;
import de.invation.code.toval.reflect.GenericReflection;
import de.invation.code.toval.validate.Validate;

public class ArrayUtils {
	
	/**
	 * Random number generator.
	 */
	private static java.util.Random rand = new Random(); 
	/**
	 * String for value separation.
* Used for generating String representations of arrays. */ public static final char VALUE_SEPARATION = ' '; /** * String representation for empty arrays. */ public static final String EMPTY_ARRAY = "[]"; /** * Default precision used for String representations of array elements * having type Float or Double. */ public static final int DEFAULT_PRECISION = 2; /** * Returns a new array with the given size containing the default value at each index. * @param size The desired size of the array. * @param defaultValue The default value to use * @return The created array */ @SuppressWarnings("unchecked") public static T[] createArray(int size, T defaultValue){ T[] result = (T[]) GenericReflection.newArray(defaultValue.getClass(), size); for(int i=0; i Type of array elements * @param arr Array * @return Random element of arr */ public static T getRandomItem(T[] arr){ return arr[rand.nextInt(arr.length)]; } /** * Reverses the entries of a given array. * @param Type of array elements * @param arr Array to reverse * @return The same array in reverse order */ public static T[] reverseArray(T[] arr) { for (int left=0, right=arr.length-1; left List toList(T[] arr){ return Arrays.asList(arr); } public static List toStringList(T[] arr){ List result = new ArrayList(); for(T t: arr){ result.add(t.toString()); } return result; } /** * Swaps the elements at position a and b in the array arr. * @param Type of array elements * @param arr Array that contains the elements to be swapped * @param a First position * @param b Second position */ public static void swap(T[] arr, int a, int b) { if(a<0 || a>arr.length || b<0 || b>arr.length) throw new IllegalArgumentException("swap position out of bounds."); if(a != b) { T t = arr[a]; arr[a] = arr[b]; arr[b] = t; } } /** * Permutes the elements of the given array. * @param Array-type * @param arr Array to shuffle */ public static void shuffleArray(T[] arr) { for(int i = arr.length; i > 1; i--) swap(arr, i-1, rand.nextInt(i)); } /** * Checks if the given array contains the specified value.
* @param Type of array elements and value * @param array Array to examine * @param value Value to search * @return true if array contains value, false otherwise */ public static boolean arrayContains(T[] array, T value) { for(int i=0; icuts
.
* Cuts are interpreted in an inclusive way, which means that a single cut at position i * divides the given array in 0...i-1 + i...n
* This method deals with both cut positions including and excluding start and end-indexes
* @param Type of array elements * @param arr The array to divide * @param cuts Cut positions for divide operations * @return A list of subarrays of arr according to the given cut positions */ public static List divideArray(T[] arr, Integer... cuts) { Arrays.sort(cuts); int c = cuts.length; if(cuts[0]<0 || cuts[c-1]>arr.length-1) throw new IllegalArgumentException("cut position out of bounds."); int startIndex = cuts[0]==0 ? 1 : 0; if(cuts[c-1]!=arr.length-1) { cuts = Arrays.copyOf(cuts, cuts.length+1); cuts[cuts.length-1] = arr.length-1; c++; } List result = new ArrayList(c-startIndex); int lastEnd = 0; for(int i=startIndex; i<=c-1; i++) { int c2 = icuts.
* Cuts are interpreted in an inclusive way, which means that a single cut at position i * divides the given array in 0...i-1 + i...n
* This method deals with both cut positions including and excluding start and end-indexes
* @param arr Array to divide * @param cuts Cut positions for divide operations * @return A list of subarrays of arr according to the given cut positions * @see #divideArray(Object[], Integer[]) */ public static List divideObjectArray(T[] arr, Integer... cuts) { return divideArray(arr, cuts); } /** * Returns an Iterator for all possible permutations of the given array. * @param Type of list array * @param arr Basic array for permutations * @return Iterator holding all possible permutations */ public static Iterator getPermutations(T[] arr){ return new ArrayPermutations(arr); } /** * Generates all permutations of a given array. * @param Type of array elements. */ private static class ArrayPermutations extends Permutations { private T[] array; public ArrayPermutations(T[] array) { super(array.length); this.array = array; } /** * Returns a new array with permuted elements. * @return A new array with permuted elements */ @Override public T[] next() { Integer[] next = super.nextPermutation(); T[] newArr = array.clone(); for (int i = 0; i < next.length; i++) { newArr[i] = array[next[i]]; } return newArr; } } /** * Returns a String representation of an object-array.
* @param arr Object-array for String representation * @return String representation of arr * @see #getFormat(Object[], int, char) */ public static String toString(T[] arr, char valueSeparation) { return toString(arr, DEFAULT_PRECISION, valueSeparation); } public static String toString(T[] arr) { return toString(arr, DEFAULT_PRECISION, VALUE_SEPARATION); } /** * Returns a String representation of an object-array.
* The specified precision is only applicable for Float and Double elements. * @param arr Object-array for String representation * @return String representation of arr * @see #getFormat(Object[], int, char) */ public static String toString(T[] arr, int precision, char valueSeparation) { if(arr.length>0) return String.format(getFormat(arr, precision, valueSeparation), arr); else return EMPTY_ARRAY; } /** * Returns a format-String that can be used to generate a String representation of an array * using the String.format method. * @param arr Array for which a String representation is desired * @param precision Desired precision for Float and Double elements * @return Format-String for arr * @see Formatter * @see String#format(String, Object...) */ private static String getFormat(T[] arr, int precision, char valueSeparation) { StringBuilder builder = new StringBuilder(); builder.append('['); for(int i=0; i * Note: Only use this method when the given arrays are sorted and contain only distinct values. * @param arrs */ public static boolean containSameElementsSorted(short[]... arrs){ Validate.notNull(arrs); if(arrs.length == 1) return true; int firstSize = arrs[0].length; for(int i=1; i boolean contains(T[] arr, T element){ for(T val: arr){ if(val.equals(element)) return true; } return false; } /** * Determines the intersection of the given arrays.
* Note: Only use this method when the given arrays are sorted and contain only distinct values. */ public static short[] intersectionSorted(short[]... arrs){ if(arrs.length == 0) return new short[0]; if(arrs.length == 1) return arrs[0]; short[][] arrList = new short[arrs.length-1][]; short[] minLengthArray = arrs[0]; for(int i=1; i commonIndices = new ArrayList(minLengthArray.length); for(short i=0; i arrList[j][k]){ if(k < arrList[j].length - 1){ pointer[j] = (short) (pointer[j] + 1); } else { break; } } else { break; } } if(arrList[j][pointer[j]] != stateIndex){ insert = false; break; } } if(insert){ commonIndices.add(stateIndex); } } short[] result = new short[commonIndices.size()]; for(int l=0; l maxValue){ maxValue = value; } } return maxValue; } public static byte min(byte[] arr){ byte minValue = Byte.MAX_VALUE; for(byte value: arr){ if(value < minValue){ minValue = value; } } return minValue; } public static MinMaxByte minMax(byte[] arr){ byte minValue = Byte.MAX_VALUE; byte maxValue = Byte.MIN_VALUE; for(byte value: arr){ if(value < minValue){ minValue = value; } if(value > maxValue){ maxValue = value; } } return new MinMaxByte(minValue, maxValue); } public static short max(short[] arr){ short maxValue = Short.MIN_VALUE; for(short value: arr){ if(value > maxValue){ maxValue = value; } } return maxValue; } public static short min(short[] arr){ short minValue = Short.MAX_VALUE; for(short value: arr){ if(value < minValue){ minValue = value; } } return minValue; } public static MinMaxShort minMax(short[] arr){ short minValue = Short.MAX_VALUE; short maxValue = Short.MIN_VALUE; for(short value: arr){ if(value < minValue){ minValue = value; } if(value > maxValue){ maxValue = value; } } return new MinMaxShort(minValue, maxValue); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy