How do I create a repeated sequence of character?

Category: java.util, viewed: 13K time(s).

This example show you how to create a repeated sequence of characters. To do this we use the Arrays.fill() method. This method fills an array of char with a character.

package org.kodejava.example.util;

import java.util.Arrays;

public class RepeatCharacter {
    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));
    }
}