Miscellaneous Utility Methods in the Arrays Class

The methods toString() and fill() (Example 15.10, Example 15.11) have previously been used in this chapter.

Click here to view code image

String toString(
type
[] array)
String deepToString(Object[] array)

Return a text representation of the contents (or “deep contents”) of the specified array. The first method calls the toString() method of the element if this element is not an array, but calls the Object.toString() method if the element is an array—that is, it creates a one-dimensional text representation of an array. The second method goes deeper and creates a multidimensional text representation for an arrays of arrays—that is, arrays that have arrays as elements. The type can be any primitive type.

Click here to view code image

void fill(
type
[] array,
type
 value)
void fill(
type
[] array, int fromIndex, int toIndex,
type
 value)

In these methods, the type can be any primitive type, or Object. The methods assign the specified value to each element of the specified array or the subarray given by the half-open interval whose index bounds are specified.

The code below illustrates how the Arrays.fill() method can be used. Each element in the local array bar is assigned the character ‘*’ using the fill() method, and the array is printed.

Click here to view code image

for (int i = 0; i < 5; ++i) {
  char[] bar = new char[i];
  Arrays.fill(bar, ‘*’);
  System.out.printf(“%d %s%n”, i, Arrays.toString(bar));
}

Output from the for(;;) loop:

0 []
1 [*]
2 [*, *]
3 [*, *, *]
4 [*, *, *, *]

The Arrays.toString() method has been used in many examples to convert the contents of an array to a text representation. The Arrays.deepToString() method can be used to convert the contents of a multidimensional array to a text representation, where each element that is an array is also converted to a text representation.

Click here to view code image

char[][] twoDimArr = new char[2][2];
Arrays.fill(twoDimArr[0], ‘*’);
Arrays.fill(twoDimArr[1], ‘*’);
System.out.println(Arrays.deepToString(twoDimArr));    // [[*, *], [*, *]]

In addition to initializing an array in an array initialization block, the following overloaded setAll() methods in the Arrays class can be used to initialize an existing array.

Click here to view code image

<T> void setAll(T[] array, IntFunction<? extends T> generator)
void setAll(int[] array, IntUnaryOperator generator)
void setAll(long[] array, IntToLongFunction generator)
void setAll(double[] array, IntToDoubleFunction generator)

Set all elements of the specified array, using the provided generator function to compute each element.

The code below illustrates how each element of an existing array can be initialized to the result of applying a function.

Click here to view code image

String[] strArr = new String[5];
Arrays.setAll(strArr, i -> “Str@” + i); // [Str@0, Str@1, Str@2, Str@3, Str@4]
int[] intArr = new int[4];
Arrays.setAll(intArr, i -> i * i);      // [0, 1, 4, 9]

Leave a Reply

Your email address will not be published. Required fields are marked *