Java Arrays fill array element value by index

Introduction

To fill array element value by index in Java

Arrays.fill(array, 2, 6, -1);  

Full source

import java.util.Arrays;

public class Main {
  public static void main(String args[]) {
    // Allocate and initialize array. 
    int array[] = new int[10];  
    for(int i = 0; i < 10; i++)  
      array[i] = -3 * i;  //from w ww .jav  a  2s. c  o  m
  
    // Display, sort, and display the array. 
    System.out.println("Original contents: "+Arrays.toString(array));  

    // Fill and display the array. 
    Arrays.fill(array, 2, 6, -1);  
    System.out.print("After fill(): ");  
    System.out.println("Original contents: "+Arrays.toString(array));  

  }
}



PreviousNext

Related