Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe for all free courses

How to fill and clear contents of an Array in Java ?.

Program to implement how to fill and clear contents of an Array in Java.

package com.hubberspot.arrays;

import java.util.Arrays;

public class ArraysFill {

 public static void main(String[] args) {

  // Create an array of Strings storing few customers names
  String[] customers = {"Dinesh" , "Jonty" , "Mallika" , "Navjinder"};

  // print out the customers name in array using Arrays class
  // static method by name toString()
  System.out.println("Customers Name: " + Arrays.toString(customers));

  // To clear contents of array at once we use 
  // Arrays class static method by name fill()
  // It replaces each element of an array by the second
  // parameters it takes in 
  Arrays.fill(customers, null);

  System.out.println("Customers Name: " + Arrays.toString(customers));

  // Here instead of null now we pass a String by name "Radhika"
  // it replaces each and every element of array to this String
  Arrays.fill(customers, "Radhika");

  // Print the array 
  System.out.println("Customers Name: " + Arrays.toString(customers));

  // Again assigning null and clearing out the contents
  Arrays.fill(customers, null);

  System.out.println("Customers Name: " + Arrays.toString(customers));

 }

}



Output of the program :




 
© 2021 Learn Java by Examples Template by Hubberspot