double array java- java double array declare initialize access

Double array java is a Java array with double values.

All values are stored in contineous memory location

Declaring double array in Java

General syntax to declare array is

dataType [] arrayName;

dataType: Any primitive or user-defined data types like int, char, float, double. etc

[] : notation to represent an array.

arrayName: name of an array;

double[] d;

double[] price;

here d and price are arrays that can refer to the array of double type.

Initializing and Accessing double array in Java

initialization specifies the size and initial values of an array.

Syntax of initialization is

dataType [] arrayName=new dataType[SIZE];

or

dataType [] arrayName=new dataType[]{value1, value2, …. ,value n};

double[] d=new double[5];

double[] price=new double[4];

above two statements will allocate memory for two array that are pointed by d and price.

Initialize both arrays by value 0.0 (the default value of double data type).

We can also provide some initial value to the array as follows

double[] d=new double[]{2.3,5.4,7.6,8.9,9.9};

public class MyDoubleArray {
    public static void main(String args[]) {
    double[] d=new double[]{2.3,5.4,7.6,8.9,9.9};

    for(double a:d){
        System.out.println(a);
    }
    }
}
2.3
5.4
7.6
8.9
9.9

another way to initialize array is

double[] d={2.3,5.4,7.6,8.9,9.9};

Java Array Loop Initialization

import java.util.Scanner;
public class MyDoubleArray {
    public static void main(String args[]) {
    double[] d=new double[5];
    
    System.out.println("Enter the values for array");
    Scanner sc=new Scanner(System.in);
    
    for(int i=0;i



Enter the values for array
12.3 
45.3 
66.5 
77.5 
78.7
Array values are
12.3
45.3
66.5
77.5
78.7

Initializing Array using Arrays.fill()

Some times we need to fill array using same value that we can use fill method as below.

double[] d=new double[5];
Arrays.fill(d,4.4);

It will fill all array elements with 4.4

double[] d={2.3,4.5,5.6,7.8,9.8};
Arrays.fill(d,1,3,5.5);

Initializing Array using Arrays.setAll()

import java.util.Arrays;
public class MyClass {
    public static void main(String args[]) {
      double[] d=new double[5];
      Arrays.setAll(d,(index)-> index + 1.1);
      System.out.println(Arrays.toString(d));
      
    }
}
Enter the values for array
[1.1, 2.1, 3.1, 4.1, 5.1]

DoubleStream to double Array in Java

DoubleStream is a sequence of double values that supports sequential and parallel aggregation operations.

DoubleStreams of() can be used to create a double stream of specified values.

toArray() of DoubleStream can use to convert it to an array.

import java.util.Arrays;
import java.util.stream.DoubleStream;
import java.util.Arrays;


public class MyClass {
    public static void main(String args[]) {
      DoubleStream ds=DoubleStream.of(7.5,66.7,88.7,333.4);
      double[] d=ds.toArray();
      System.out.println(Arrays.toString(d));
    }
}
Enter the values for array
[7.5, 66.7, 88.7, 333.4]

double array Java length

The length of an array can be found using the length property.

public class MyClass {
    public static void main(String args[]) {
        double[] d={4.4,5.2,6.3,7.8};
        System.out.println("Array Length "+d.length);
    }
}
Enter the values for array
Array Length 4

double to double array copy

To copy one array in to another we can use

  1. clone()
  2. System.arraycopy()
  3. Arrays.copyOf()

there are other methods also we discuss above three here

double array copy using clone()

import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
        double[] d={4.4,5.2,6.3,7.8};
       double[] e=d.clone();
       System.out.println(Arrays.toString(e));
    }
}

array copy using System.arraycopy()

import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
        double[] d={4.4,5.2,6.3,7.8};
       double[] e=new double[d.length];
       System.arraycopy(d,0,e,0,d.length);
       System.out.println(Arrays.toString(e));
    }
}

double array copy using Arrays.copyOf()

import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
        double[] d={4.4,5.2,6.3,7.8};
       double[] e=Arrays.copyOf(d,d.length);
       System.out.println(Arrays.toString(e));
    }
}

Above all will copy double array d to e.

Enter the values for array
[4.4, 5.2, 6.3, 7.8]

Sorting double Array

Arrays class provides a method sort() to sort any type of array.

example to sort double array is as below.

import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
        double[] d={6.6,5.5,7.8,1.1,2.1,4.3,7.1};
       Arrays.sort(d);
       System.out.println(Arrays.toString(d));
    }
}

output of above program is here

[1.1, 2.1, 4.3, 5.5, 6.6, 7.1, 7.8]

There are different ways to sort and array we discussed only one.