Skip to content

Array Extensions

Andrzej Kebab edited this page Feb 4, 2024 · 11 revisions

UtilityLibrary.Core

IsNullOrEmpty

Checks if the array is null or empty.

using UtilityLibrary.Core;

int[] numbers = { 1, 2, 3, 4, 5 };

bool isNullOrEmpty = numbers.IsNullOrEmpty();

// Result: false

IsEmpty

Checks if the array is empty, throwing an exception if it is null.

using UtilityLibrary.Core;

int[] numbers = { 1, 2, 3, 4, 5 };

bool isEmpty = numbers.IsEmpty();

// Result: false

WithinIndex

Checks if the given index is within the bounds of the array.

using UtilityLibrary.Core;

int[] numbers = { 1, 2, 3, 4, 5 };

bool withinIndex = numbers.WithinIndex(3);

// Result: true

WithinIndex (with dimension)

Checks if the given index is within the bounds of the specified dimension of the array.

using UtilityLibrary.Core;

int[,] matrix = { { 1, 2, 3 }, { 4, 5, 6 } };

bool withinIndex = matrix.WithinIndex(1, 2);

// Result: true

Swap

Swaps the elements at the specified indices in the array.

using UtilityLibrary.Core;

int[] numbers = { 1, 2, 3, 4, 5 };

numbers = numbers.Swap(1, 3);

// Result: [1, 4, 3, 2, 5]

Find

Finds all elements in the array that match the conditions defined by the specified predicate.

using UtilityLibrary.Core;

int[] numbers = { 1, 2, 3, 4, 5 };

List<int> evenNumbers = numbers.Find(x => x % 2 == 0);

// Result: [2, 4]

IndexOf

Searches for the specified object and returns the index of its first occurrence in a one-dimensional array.

using UtilityLibrary.Core;

string[] names = { "John", "Doe", "Jane", "Doe" };

int indexOfDoe = names.IndexOf("Doe");

// Result: 1

CombineArray

Combines two arrays into one.

using UtilityLibrary.Core;

int[] firstArray = { 1, 2, 3 };
int[] secondArray = { 4, 5, 6 };

int[] combinedArray = firstArray.CombineArray(secondArray);

// Result: [1, 2, 3, 4, 5, 6]

ClearAt

Clears the value at the specified index in the array.

using UtilityLibrary.Core;

int[] numbers = { 1, 2, 3, 4, 5 };

numbers = numbers.ClearAt(2);

// Result: [1, 2, 0, 4, 5]

ClearAll

Clears all values in the array.

using UtilityLibrary.Core;

int[] numbers = { 1, 2, 3, 4, 5 };

numbers = numbers.ClearAll();

// Result: [0, 0, 0, 0, 0]

BlockCopy

Copies a specified number of elements from a source array starting at a particular offset to a new array.

using UtilityLibrary.Core;

int[] numbers = { 1, 2, 3, 4, 5 };

int[] copiedArray = numbers.BlockCopy(1, 3);

// Result: [2, 3, 4]

BlockCopy (Multiple)

Copies elements from a source array into a collection of arrays, each of a specified length.

using UtilityLibrary.Core;

int[] numbers = { 1, 2, 3, 4, 5 };

List<int[]> copiedArrays = numbers.BlockCopy(2).ToList();

// Result: [[1, 2], [3, 4], [5]]

AsObjects

Converts an array of a specific type to an array of objects.

using UtilityLibrary.Core;

int[] numbers = { 1, 2, 3, 4, 5 };

object[] objects = numbers.AsObjects();

// Result: [1, 2, 3, 4, 5] (as objects)

Sort

Sort<T>(Comparison<T> comparison)

Sorts the elements in the entire array using the specified comparison.

// Example array of integers
int[] intArray = { 4, 2, 7, 1, 9 };

// Using the Sort method to sort the array with a custom comparison
intArray.Sort((a, b) => a.CompareTo(b));

// Displaying the sorted array
Console.WriteLine($"Sorted array: {string.Join(", ", intArray)}");
// Output: Sorted array: 1, 2, 4, 7, 9

Parameters:

  • array (T[]): The one-dimensional array to sort.
  • comparison (Comparison<T>): The comparison to use when comparing elements.

Returns:

  • T[]: The sorted array.

Sort<T>(int index, int length, IComparer<T> comparer)

Sorts a range of elements in the array using the specified comparer.

// Example array of strings
string[] stringArray = { "apple", "orange", "banana", "grape" };

// Using the Sort method to sort a specific range with a custom comparer
stringArray.Sort(1, 3, StringComparer.Length);

// Displaying the sorted array
Console.WriteLine($"Sorted array: {string.Join(", ", stringArray)}");
// Output: Sorted array: grape, apple, orange, banana

Parameters:

  • array (T[]): The one-dimensional array to sort.
  • index (int): The zero-based starting index of the range to sort.
  • length (int): The length of the range to sort.
  • comparer (IComparer<T>): The comparer to use when comparing elements.

Returns:

  • T[]: The sorted array.

RandSort<T>()

Sorts the elements in the array in random order.

// Example array of characters
char[] charArray = { 'a', 'b', 'c', 'd', 'e' };

// Using the RandSort method to sort the array in random order
charArray.RandSort();

// Displaying the array with elements in random order
Console.WriteLine($"Array with elements in random order: {string.Join(", ", charArray)}");
// Output: Array with elements in random order: b, e, a, d, c

Parameters:

  • array (T[]): The one-dimensional array to sort randomly.

Returns:

  • T[]: The array with elements in random order.

FlattenIndex

FlattenIndex for a Two-Dimensional Array

Calculates the flattened index for a two-dimensional array.

Array array = // initialize your two-dimensional array
int x = // index along the first dimension
int y = // index along the second dimension
int flattenedIndex = array.FlattenIndex(x, y);

Parameters:

  • array: The two-dimensional array.
  • x: The index along the first dimension.
  • y: The index along the second dimension.

Returns:

  • flattenedIndex: The flattened index.

FlattenIndex for a Three-Dimensional Array

Calculates the flattened index for a three-dimensional array.

Array array = // initialize your three-dimensional array
int x = // index along the first dimension
int y = // index along the second dimension
int z = // index along the third dimension
int flattenedIndex = array.FlattenIndex(x, y, z);

Parameters:

  • array: The three-dimensional array.
  • x: The index along the first dimension.
  • y: The index along the second dimension.
  • z: The index along the third dimension.

Returns:

  • flattenedIndex: The flattened index.

Get

GetAtFlatIndex

Retrieves the element at the specified 2D index in a flattened array.

var array = new int[] { 1, 2, 3, 4 };
int width = 2;
int x = 1;
int y = 0;

var result = array.GetAtFlatIndex(width, x, y);
// Output: 3

Parameters:

  • array: The one-dimensional array to retrieve the element from.
  • width: The width of the original two-dimensional array.
  • x: The x-coordinate in the original two-dimensional array.
  • y: The y-coordinate in the original two-dimensional array.

Returns:

  • object: The element at the specified 2D index in the flattened array.

GetAtFlatIndex

Retrieves the element at the specified 3D index in a flattened array.

var array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
int width = 2;
int height = 2;
int x = 1;
int y = 0;
int z = 1;

var result = array.GetAtFlatIndex(width, height, x, y, z);
// Output: 7

Parameters:

  • array: The one-dimensional array to retrieve the element from.
  • width: The width of the original three-dimensional array.
  • height: The height of the original three-dimensional array.
  • x: The x-coordinate in the original three-dimensional array.
  • y: The y-coordinate in the original three-dimensional array.
  • z: The z-coordinate in the original three-dimensional array.

Returns:

  • object: The element at the specified 3D index in the flattened array.

GetAtFlatIndex

Retrieves the element at the specified 3D index in a flattened array. This method assumes that the original three-dimensional array was a cube, with equal width, height, and depth.

var array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
int size = 2;
int x = 1;
int y = 0;
int z = 1;

var result = array.GetAtFlatIndex(size, x, y, z);
// Output: 7

Parameters:

  • array: The one-dimensional array to retrieve the element from.
  • size: The size (width, height, and depth) of the original three-dimensional array.
  • x: The x-coordinate in the original three-dimensional array.
  • y: The y-coordinate in the original three-dimensional array.
  • z: The z-coordinate in the original three-dimensional array.

Returns:

  • object: The element at the specified 3D index in the flattened array.

Flatten / Unflatten

FlattenFrom2D

Flattens a two-dimensional array to a one-dimensional array.

T[,] input = // initialize your two-dimensional array
T[] flattened = input.FlattenFrom2D();

Parameters:

  • input: The two-dimensional array.

Returns:

  • flattened: The flattened one-dimensional array.

UnflattenTo2D

Unflattens a one-dimensional array to a two-dimensional array.

T[] input = // initialize your flattened one-dimensional array
int width = // width of the two-dimensional array
int height = // height of the two-dimensional array
T[,] unflattened = input.UnflattenTo2D( width, height);

Parameters:

  • input: The flattened one-dimensional array.
  • width: Width of the two-dimensional array.
  • height: Height of the two-dimensional array.

Returns:

  • unflattened: The unflattened two-dimensional array.

UtilityLibrary.Core.LinqReplacement

First

Returns the first element of an array, or a default value if the array contains no elements.

using UtilityLibrary.Core.LinqReplacement;

int[] numbers = { 1, 2, 3, 4, 5 };

int firstNumber = numbers.First();

// Result: 1

First with Predicate

Returns the first element of an array that satisfies a specified condition, or a default value if no such element is found.

using UtilityLibrary.Core.LinqReplacement;

int[] numbers = { 1, 2, 3, 4, 5 };

int firstEvenNumber = numbers.First(x => x % 2 == 0);

// Result: 2

Last

Returns the last element of an array, or a default value if the array contains no elements.

using UtilityLibrary.Core.LinqReplacement;

int[] numbers = { 1, 2, 3, 4, 5 };

int lastNumber = numbers.Last();

// Result: 5

Last with Predicate

Returns the last element of an array that satisfies a specified condition, or a default value if no such element is found.

using UtilityLibrary.Core.LinqReplacement;

int[] numbers = { 1, 2, 3, 4, 5 };

int lastEvenNumber = numbers.Last(x => x % 2 == 0);

// Result: 4

Contains

Determines whether an array contains a specific value.

using UtilityLibrary.Core.LinqReplacement;

string[] fruits = { "apple", "banana", "orange" };

bool containsBanana = fruits.Contains("banana");

// Result: true

Contains with Predicate

Determines whether an array contains elements that satisfy a specified condition.

using UtilityLibrary.Core.LinqReplacement;

int[] numbers = { 1, 2, 3, 4, 5 };

bool containsEvenNumber = numbers.Contains(x => x % 2 == 0);

// Result: true

Clone this wiki locally