Java Program to fill elements in a long array

Java 8Object Oriented ProgrammingProgramming

Elements can be filled in a long array using the java.util.Arrays.fill() method. This method assigns the required long value to the long array in Java. The two parameters required are the array name and the value that is to be stored in the array elements.

A program that demonstrates this is given as follows -

Example

 Live Demo

import java.util.Arrays;
public class Demo {
   public static void main(String[] argv) throws Exception {
      long[] longArray = new long[5];
      long longValue = 125;
      Arrays.fill(longArray, longValue);
      System.out.println("The long array content is: " + Arrays.toString(longArray));
   }
}

Output

The long array content is: [125, 125, 125, 125, 125

Now let us understand the above program

First the long array longArray[] is defined. Then the value 125 is filled in the long array using the Arrays.fill() method. Finally, the long array is printed using the Arrays.toString() method. A code snippet which demonstrates this is as follows:

long[] longArray = new long[5];
long longValue = 125;
Arrays.fill(longArray, longValue);
System.out.println("The long array content is: " + Arrays.toString(longArray));
raja
Published on 09-Jan-2019 12:29:50
Advertisements