Tuesday, March 17, 2015

To fill (initialize at once) an array in java pgm



To fill (initialize at once) an array
 
import java.util.*;
public class FillTest 
{
   public static void main(String args[]) 
{
      int array[] = new int[6];
      Arrays.fill(array, 100);
      for (int i=0, n=array.length; i < n; i++) 
{
         System.out.println(array[i]);
      }
      System.out.println();
      Arrays.fill(array, 3, 6, 50);
      for (int i=0, n=array.length; i< n; i++)
 {
         System.out.println(array[i]);
      }
   }
}

Result:

100
 
100
100
100
100
100
 
100
100
100
50
50
50

No comments:

Post a Comment