Package: primitives

## [Java] Class char[]

- primitives.char[]

-     public class char[]
      extends Object

  GDK enhancements for char[].

  displayName:
    char[]
  receiverFqcn:
    char[]

- - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public boolean` | `any(Closure<?> predicate)` Iterates over the contents of a char Array, and checks whether a predicate is valid for at least one element.
    `` | `public boolean` | `asBoolean()` Coerces a char array to a boolean value.
    `` | `public List<List<Character>>` | `chop(int chopSizes)` Chops the char array into pieces, returning lists with sizes corresponding to the supplied chop sizes.
    `` | `public boolean` | `contains(Object value)` Checks whether the array contains the given value.
    `` | `public Number` | `count(Object value)` Counts the number of occurrences of the given value inside this array.
    `` | `public char[]` | `each(Consumer<Character> consumer)` Iterates through a char[] passing each char to the given consumer.
    `` | `public char[]` | `each(Closure<?> closure)` Iterates through a char[] passing each char to the given closure.
    `` | `public char[]` | `eachWithIndex(Closure<?> closure)` Iterates through a char[], passing each char and the element's index (a counter starting at zero) to the given closure.
    `` | `public boolean` | `equals(char[] right)` Compares the contents of this array to the contents of the given array.
    `` | `public boolean` | `every(Closure<?> predicate)` Iterates over the contents of a char Array, and checks whether a predicate is valid for all elements.
    `` | `public char` | `first()` Returns the first item from the char array.
    `` | `public List<Character>` | `flatten()` Flattens an array.
    `` | `public List<Character>` | `getAt(Range<?> range)` Supports the subscript operator for a char array with a range giving the desired indices.
    `` | `public List<Character>` | `getAt(IntRange range)` Supports the subscript operator for a char array with an IntRange giving the desired indices.
    `` | `public List<Character>` | `getAt(ObjectRange range)` Supports the subscript operator for a boolean array with an ObjectRange giving the desired indices.
    `` | `public List<Character>` | `getAt(Collection<?> indices)` Supports the subscript operator for a char array with a (potentially nested) collection giving the desired indices.
    `` | `public IntRange` | `getIndices()` Returns indices of the char array.
    `` | `public String` | `groovyToString()` Returns Groovy's string representation for a char array.
    `` | `public char` | `head()` Returns the first item from the char array.
    `` | `public char[]` | `init()` Returns the items from the char array excluding the last item.
    `` | `public String` | `join()` Concatenates the string representation of each item in this array.
    `` | `public String` | `join(String separator)` Concatenates the string representation of each item in this array, with the given String as a separator between each item.
    `` | `public char` | `last()` Returns the last item from the char array.
    `` | `public int` | `partitionPoint(IntRange range, IntPredicate condition)` Returns the index of the partition point according to the given predicate (the index of the first element of the second partition).
    `` | `public int` | `partitionPoint(IntPredicate condition)` Returns the index of the partition point according to the given predicate (the index of the first element of the second partition).
    `` | `public void` | `putAt(IntRange range, char[] source)` A helper method to allow arrays to be set with subscript operators.
    `` | `public void` | `putAt(IntRange range, Iterable<Character> source)` A helper method to allow arrays to be set with subscript operators.
    `` | `public char[]` | `reverse()` Creates a new char array containing items which are the same as this array but in reverse order.
    `` | `public char[]` | `reverse(boolean mutate)` Reverses the items in an array.
    `` | `public char[]` | `reverseEach(Closure<?> closure)` Iterates through a char[] in reverse order passing each char to the given closure.
    `` | `public int` | `size()` Provides arrays with a `size` method similar to collections.
    `` | `public Stream<Character>` | `stream()` Returns a sequential Stream with the specified array as its source.
    `` | `public char` | `sum()` Sums the items in an array.
    `` | `public char` | `sum(char initialValue)` Sums the items in an array, adding the result to some initial value.
    `` | `public char[]` | `swap(int i, int j)` Swaps two elements at the specified positions.
    `` | `public char[]` | `tail()` Returns the items from the char array excluding the first item.
    `` | `public List<Character>` | `toImmutableList()` Converts this array to an immutable List of the same size, with each element added to the list.
    `` | `public Set<Character>` | `toImmutableSet()` Converts this array to an immutable Set, with each unique element added to the set.
    `` | `public List<Character>` | `toList()` Converts this array to a List of the same size, with each element added to the list.
    `` | `public Set<Character>` | `toSet()` Converts this array to a Set, with each unique element added to the set.
    `` | `public String` | `toString()` Returns the string representation of the given array.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class Object` | `addShutdownHook, any, any, asBoolean, asType, clamp, clamp, collect, collect, collect, dump, each, eachMatch, eachMatch, eachWithIndex, every, every, find, find, findAll, findAll, findIndexOf, findIndexOf, findIndexValues, findIndexValues, findLastIndexOf, findLastIndexOf, findResult, findResult, findResult, findResult, getAt, getMetaClass, getMetaPropertyValues, getProperties, grep, grep, hasProperty, identity, inject, inject, inspect, invokeMethod, is, isCase, isNotCase, iterator, metaClass, print, print, printf, printf, println, println, println, putAt, respondsTo, respondsTo, setMetaClass, sleep, sleep, split, sprintf, sprintf, stream, takeIf, tap, toString, use, use, use, with, with, withCloseable, withCloseable, withMethodClosure, withStream, withStream, withTraits`

- - ### Method Detail

    - #### public boolean any(Closure<?> predicate)

      Iterates over the contents of a char Array, and checks whether a predicate is valid for at least one element.

           char[] array = ['a', 'b', 'c']
           assert array.any{ it <= 'a' }
           assert !array.any{ it < 'a' }

      Parameters:
        `predicate` - the closure predicate used for matching
      Returns:
        true if any iteration for the chars matches the closure predicate
      Since:
        5.0.0

    - #### public boolean asBoolean()

      Coerces a char array to a boolean value. A char array is false if the array is of length 0, and true otherwise.

      Returns:
        the array's boolean value
      Since:
        1.7.4

    - #### public List<List<Character>> chop(int chopSizes)

      Chops the char array into pieces, returning lists with sizes corresponding to the supplied chop sizes. If the array isn't large enough, truncated (possibly empty) pieces are returned. Using a chop size of -1 will cause that piece to contain all remaining items from the array.

           char[] array = [0, 1, 2]
           assert array.chop(1, 2) == [[0], [1, 2]]

      Parameters:
        `chopSizes` - the sizes for the returned pieces
      Returns:
        a list of lists chopping the original array elements into pieces determined by chopSizes
      See Also:
        ArrayGroovyMethods#collate(Object[], int)#collate(Object[], int)
      Since:
        5.0.0

    - #### public boolean contains(Object value)

      Checks whether the array contains the given value.

      Parameters:
        `value` - the value being searched for
      Returns:
        true if the array contains the value
      Since:
        1.8.6

    - #### public Number count(Object value)

      Counts the number of occurrences of the given value inside this array. Comparison is done using Groovy's == operator (using `compareTo(value) == 0`).

           char[] array = ['x', 'y', 'z', 'z', 'y']
           assert array.count('y') == 2

      Parameters:
        `value` - the value being searched for
      Returns:
        the number of occurrences
      See Also:
        DefaultGroovyMethods#count(Iterator, Object)#count(Iterator, Object)
      Since:
        1.6.4

    - #### public char[] each(Consumer<Character> consumer)

      Iterates through a char[] passing each char to the given consumer.

           char[] array = ['a' as char, 'b' as char, 'c' as char]
           String result = ''
           array.each{ result += it }
           assert result == 'abc'

      Parameters:
        `consumer` - the consumer for each char
      Returns:
        the self array
      Since:
        5.0.0

    - #### public char[] each(Closure<?> closure)

      Iterates through a char[] passing each char to the given closure.

           char[] array = ['a' as char, 'b' as char, 'c' as char]
           String result = ''
           array.each{ result += it }
           assert result == 'abc'

      Parameters:
        `closure` - the closure applied on each char
      Returns:
        the self array
      Since:
        6.0.0

    - #### public char[] eachWithIndex(Closure<?> closure)

      Iterates through a char[], passing each char and the element's index (a counter starting at zero) to the given closure.

           char[] array = ['a' as char, 'b' as char, 'c' as char]
           String result = ''
           array.eachWithIndex{ item, index -> result += "$index($item)" }
           assert result == '0(a)1(b)2(c)'

      Parameters:
        `closure` - a Closure to operate on each char
      Returns:
        the self array
      Since:
        5.0.0

    - #### public boolean equals(char[] right)

      Compares the contents of this array to the contents of the given array.

      Example usage:

           char[] array1 = ['a', 'b']
           char[] array2 = ['a', 'b']
           assert array1 !== array2
           assert array1.equals(array2)

      Parameters:
        `right` - the array being compared
      Returns:
        true if the contents of both arrays are equal.
      Since:
        5.0.0

    - #### public boolean every(Closure<?> predicate)

      Iterates over the contents of a char Array, and checks whether a predicate is valid for all elements.

           char[] array = ['a', 'b', 'c']
           assert array.every{ it <= 'd' }
           assert !array.every{ it > 'b' }

      Parameters:
        `predicate` - the closure predicate used for matching
      Returns:
        true if the predicate returns true for all elements in the array
      Since:
        5.0.0

    - #### public char first()

      Returns the first item from the char array.

           char[] chars = ['a', 'b', 'c']
           assert chars.first() == 'a'

      An alias for `head()`.
      throws:
        NoSuchElementException if the array is empty
      Returns:
        the first element
      Since:
        5.0.0

    - #### public List<Character> flatten()

      Flattens an array. This array is added to a new collection. It is an alias for `toList()` but allows algorithms to be written which also work on multidimensional arrays or non-arrays where flattening would be applicable.

           char[] array = 'ab'.chars
           assert array.flatten() == ['a', 'b']

      Returns:
        a Collection of the array elements
      Since:
        1.6.0

    - #### public List<Character> getAt(Range<?> range)

      Supports the subscript operator for a char array with a range giving the desired indices.

           char[] array = 'abcdef'.chars
           assert array[2..<2] == [] // EmptyRange
           assert array[(0..5.5).step(2)] == ['a', 'c', 'e']  // NumberRange
           assert array[(1..5.5).step(2)] == ['b', 'd', 'f']  // NumberRange

      Parameters:
        `range` - a range indicating the indices for the items to retrieve
      Returns:
        list of the retrieved chars
      Since:
        1.5.0

    - #### public List<Character> getAt(IntRange range)

      Supports the subscript operator for a char array with an IntRange giving the desired indices.

           char[] array = 'abcdef'.chars
           assert array[2..3] == ['c', 'd']
           assert array[-2..-1] == ['e', 'f']
           assert array[-1..-2] == ['f', 'e']

      Parameters:
        `range` - an IntRange indicating the indices for the items to retrieve
      Returns:
        list of the retrieved chars
      Since:
        1.0

    - #### public List<Character> getAt(ObjectRange range)

      Supports the subscript operator for a boolean array with an ObjectRange giving the desired indices.

           char[] array = 'abcdef'.chars
           def range = new ObjectRange(2, 3)
           assert array[range] == ['c', 'd']

      Parameters:
        `range` - an ObjectRange indicating the indices for the items to retrieve
      Returns:
        list of the retrieved chars
      Since:
        1.0

    - #### public List<Character> getAt(Collection<?> indices)

      Supports the subscript operator for a char array with a (potentially nested) collection giving the desired indices.

           char[] array = 'abcde'.chars
           assert array[2, 3] == ['c', 'd']
           assert array[1, 0..1, [0, [-1]]] == ['b', 'a', 'b', 'a', 'e']

      Parameters:
        `indices` - a collection of indices for the items to retrieve
      Returns:
        list of the chars at the given indices
      Since:
        1.0

    - #### public IntRange getIndices()

      Returns indices of the char array.

           char[] array = 'ab'.chars
           assert array.indices == 0..1

      See Also:
        ArrayGroovyMethods#getIndices(Object[])#getIndices(Object[])
      Since:
        3.0.8

    - #### public String groovyToString()

      Returns Groovy's string representation for a char array. By default, a char array is rendered as a String (e.g. `'abc'.chars` displays as `abc`).

      If disabled, e.g. via `-Dgroovy.extension.disable=groovyToString(char[])`, the JDK's default array `toString()` is used instead.

      Returns:
        the string representation
      Since:
        6.0.0

    - #### public char head()

      Returns the first item from the char array.

           char[] chars = ['a', 'b', 'c']
           assert chars.head() == 'a'

      An alias for `first()`.
      throws:
        NoSuchElementException if the array is empty
      Returns:
        the first element
      Since:
        5.0.0

    - #### public char[] init()

      Returns the items from the char array excluding the last item.

           char[] chars = ['a', 'b', 'c']
           def result = chars.init()
           assert result == ['a', 'b']
           assert chars.class.componentType == result.class.componentType

      throws:
        UnsupportedOperationException if the array is empty
      Returns:
        an array without its last element
      Since:
        5.0.0

    - #### public String join()

      Concatenates the string representation of each item in this array.

      Returns:
        the joined String
      Since:
        5.0.0

    - #### public String join(String separator)

      Concatenates the string representation of each item in this array, with the given String as a separator between each item.

      Parameters:
        `separator` - a String separator
      Returns:
        the joined String
      Since:
        2.4.1

    - #### public char last()

      Returns the last item from the char array.

           char[] chars = ['a', 'b', 'c']
           assert chars.last() == 'c'

      throws:
        NoSuchElementException if the array is empty
      Returns:
        the last element
      Since:
        5.0.0

    - #### public int partitionPoint(IntRange range, IntPredicate condition)

      Returns the index of the partition point according to the given predicate (the index of the first element of the second partition). The arr is assumed to be partitioned according to the given predicate.

           def arr = [7, 15, 3, 5, 4, 12, 6] as char[]
           assert arr.partitionPoint(0..<arr.size()) { it%2 != 0 } == 4

           def arr = [1, 2, 3, 3, 4, 4, 5, 6, 7] as char[]
           // usage case like lower_bound(cpp), bisect_left(python)
           assert arr.partitionPoint(0..<arr.size()) { it < 4 } == 4
           // usage case like upper_bound(cpp), bisect_right(python)
           assert arr.partitionPoint(0..<arr.size()) { it <= 4 } == 6
           // for all match condition
           assert arr.partitionPoint(0..<arr.size()) { it <= 100 } == arr.size()
           // for no match condition
           assert arr.partitionPoint(0..<arr.size()) { it <= 0 } == 0
           // for no match condition with range
           assert arr.partitionPoint(2..<arr.size()) { it <= 0 } == 2

      Parameters:
        `range` - the range [l,r] to find data match the condition
        `condition` - the matching condition
      Returns:
        an integer that is the index of the first element of the second partition
      Since:
        5.0.0

    - #### public int partitionPoint(IntPredicate condition)

      Returns the index of the partition point according to the given predicate (the index of the first element of the second partition). The arr is assumed to be partitioned according to the given predicate.

           def arr = [7, 15, 3, 5, 4, 12, 6] as char[]
           assert arr.partitionPoint{ it%2 != 0 } == 4

           def arr = [1, 2, 3, 3, 4, 4, 5, 6, 7] as char[]
           // usage case like lower_bound(cpp), bisect_left(python)
           assert arr.partitionPoint{ it < 4 } == 4
           // usage case like upper_bound(cpp), bisect_right(python)
           assert arr.partitionPoint{ it <= 4 } == 6
           // for all match condition
           assert arr.partitionPoint{ it <= 100 } == arr.size()
           // for no match condition
           assert arr.partitionPoint{ it <= 0 } == 0
           // empty input
           assert (new char[0]).partitionPoint{ it < 4 } == 0

      Parameters:
        `condition` - the matching condition
      Returns:
        an integer that is the index of the first element of the second partition
      Since:
        5.0.0

    - #### public void putAt(IntRange range, char[] source)

      A helper method to allow arrays to be set with subscript operators.

           char[] from = 'XYZ'.chars
           char[] to = 'abcde'.chars
           to[1..3] = from
           assert to == 'aXYZe'.chars

      throws:
        IndexOutOfBoundsException if the source doesn't have sufficient elements or the IntRange is too big for the self array
      Parameters:
        `range` - the subset of the array to set
        `source` - the source array from which new values will come
      Since:
        5.0.0

    - #### public void putAt(IntRange range, Iterable<Character> source)

      A helper method to allow arrays to be set with subscript operators. The null character, \0, will be used if the source iterable has insufficient elements.

           def from = 'XYZ'.chars.toList()
           char[] to = 'abcde'.chars
           to[1..3] = from
           assert to == 'aXYZe'.chars

      Parameters:
        `range` - the subset of the array to set
        `source` - the source array from which new values will come
      Since:
        5.0.0

    - #### public char[] reverse()

      Creates a new char array containing items which are the same as this array but in reverse order.

           char[] array = ['a', 'b']
           assert array.reverse() == ['b', 'a'] as char[]

      Returns:
        an array containing the reversed items
      See Also:
        reverse(char[], boolean)
      Since:
        5.0.0

    - #### public char[] reverse(boolean mutate)

      Reverses the items in an array. If mutate is true, the original array is modified in place and returned. Otherwise, a new array containing the reversed items is produced.

           char[] array = ['a', 'b', 'c']
           def yarra = array.reverse(true)
           assert array == ['c', 'b', 'a']
           assert yarra == ['c', 'b', 'a']
           assert array === yarra
           yarra = array.reverse(false)
           assert array !== yarra
           assert array == ['c', 'b', 'a']
           assert yarra == ['a', 'b', 'c']

      Parameters:
        `mutate` - `true` if the array itself should be reversed in place, `false` if a new array should be created
      Returns:
        an array containing the reversed items
      Since:
        5.0.0

    - #### public char[] reverseEach(Closure<?> closure)

      Iterates through a char[] in reverse order passing each char to the given closure.

           char[] array = 'abc'.chars
           String result = ''
           array.reverseEach{ result += it }
           assert result == 'cba'

      Parameters:
        `closure` - the closure applied on each char
      Returns:
        the self array
      Since:
        5.0.0

    - #### public int size()

      Provides arrays with a `size` method similar to collections.

      Returns:
        the length of the array
      Since:
        1.0

    - #### public Stream<Character> stream()

      Returns a sequential Stream with the specified array as its source.

      Returns:
        a `Stream` for the array
      Since:
        2.5.0

    - #### public char sum()

      Sums the items in an array.

          assert (1+2+3+4 as char) == ([1,2,3,4] as char[]).sum()

      Returns:
        The sum of all the items
      Since:
        2.4.2

    - #### public char sum(char initialValue)

      Sums the items in an array, adding the result to some initial value.

          assert (5+1+2+3+4 as char) == ([1,2,3,4] as char[]).sum(5 as char)

      Parameters:
        `initialValue` - the items in the array will be summed to this initial value
      Returns:
        The sum of all the items.
      Since:
        2.4.2

    - #### public char[] swap(int i, int j)

      Swaps two elements at the specified positions.

      Example:

           assert ([1, 3, 2, 4] as char[]) == ([1, 2, 3, 4] as char[]).swap(1, 2)

      Parameters:
        `i` - a position
        `j` - a position
      Returns:
        self
      Since:
        2.4.0

    - #### public char[] tail()

      Returns the items from the char array excluding the first item.

           char[] chars = ['a', 'b', 'c']
           def result = chars.tail()
           assert result == ['b', 'c']
           assert chars.class.componentType == result.class.componentType

      throws:
        UnsupportedOperationException if the array is empty
      Returns:
        an array without its first element
      Since:
        5.0.0

    - #### public List<Character> toImmutableList()

      Converts this array to an immutable List of the same size, with each element added to the list.

      Returns:
        An immutable list containing the contents of this array.
      Since:
        6.0.0

    - #### public Set<Character> toImmutableSet()

      Converts this array to an immutable Set, with each unique element added to the set.

           char[] array = 'xyzzy'.chars
           Set expected = ['x', 'y', 'z']
           assert array.toImmutableSet() == expected

      Returns:
        An immutable set containing the unique contents of this array.
      Since:
        6.0.0

    - #### public List<Character> toList()

      Converts this array to a List of the same size, with each element added to the list.

      Returns:
        A list containing the contents of this array.
      Since:
        1.0

    - #### public Set<Character> toSet()

      Converts this array to a Set, with each unique element added to the set.

           char[] array = 'xyzzy'.chars
           Set expected = ['x', 'y', 'z']
           assert array.toSet() == expected

      Returns:
        A set containing the unique contents of this array.
      Since:
        1.8.0

    - #### public String toString()

      Returns the string representation of the given array.

           char[] array = 'abcd'.chars
           assert array instanceof char[]
           assert array.toString() == 'abcd'

      Returns:
        the string representation
      Since:
        1.6.0