tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > Core > Arrays > Arrays Fill

Arrays Fill 

The following example shows how to use Arrays.fill API. We can either fill the entire array with specified character or just a specified range. Here fromIndex is the index of the first element (inclusive) to be filled with the specified value and toIndex is the index of the last element (exclusive) to be filled with the specified value.

File Name  :  
Author  :  Sudhakar KV
Email  :  [email protected]
package com.bethecoder.tutorials.utils.arrays;

import java.util.Arrays;

public class ArraysFill {

  public static void main (String [] args) {
    char [] charArray = new char [6];
    System.out.println("Before fill : " + Arrays.toString(charArray));
    Arrays.fill(charArray, 'a');
    System.out.println("Filled array : " + Arrays.toString(charArray));
    Arrays.fill(charArray, 15'b');
    System.out.println("Filled array : " + Arrays.toString(charArray));

It gives the following output,
Before fill : [ø, ø, ø, ø, ø, ø]
Filled array : [a, a, a, a, a, a]
Filled array : [a, b, b, b, b, a]


bl  br