Fill an array with a repeated sequence of character in Java
Description
The following code shows how to fill an array with a repeated sequence of character.
Example
// w w w . j a v a 2 s. c o m
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
char c = 'x';
int length = 10;
// creates char array with 10 elements
char[] chars = new char[length];
// fill each element of chars array with 'x'
Arrays.fill(chars, c);
// print out the repeated 'x'
System.out.println(String.valueOf(chars));
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »