You can use the
fill( )method to
fill inall or
a
part of an
array:
Arrays.fill(arrayName, val): fill whole array with val
Arrays.fill(arrayName, s , e , val): fill elems [s]-[e−1] with val
Example:
import java.util.Arrays;
public class myProg
{
public static void main(String[] args)
{
int[] list = new int[10];
Arrays.fill(list, 4); // Fill whole array with the value 4
Arrays.fill(list, 2 , 5 , 7); // Fill list[2-4] with the value 7
}
}
DEMO:
demo/08-array/14-arrays-class/Fill.java
Convert
all values in an array into a
single
string
(for printing without using a loop)
The
Arrays.toString(arrayName)method to
returns
a string representation
of
all elements
in the array:
Arrays.toString(arrayName): return a string of
all elements in array
Example:2 ways to
print
an array:
public static void main(String[] args)
{
int[] list = {1, 2, 3, 4};
for (int i = 0; i < list.length; i++) // Method 1 to print array
System.out.println( list[i] );
// Method 2 to print array
String s;
s = Arrays.toString(list); // Convert all elements to a string
System.out.println(s);
}