Pin It

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 :




Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot