This time I will write about array variables.
The array is a collection of multiple values. An array variable is a method of putting it in a variable and using it.
Click here for an example
Example of array variable
public class Sample {
public static void main(String[] args) {
double[] d = new double[3];
double sum,avg; //Variable to put total value and average value
//Assign value
d[0] = 1.2;
d[1] = 3.7;
d[2] = 4.1;
sum = 0.0;
for(int i = 0; i < d.length; i++){
System.out.print(d[i] + " ");
sum += d[i]; //You can sum the values with iterative syntax.
}
System.out.println();
avg = sum / 3.0;
System.out.println("Total value:" + sum);
System.out.println("Average value:" + avg);
}
}
The execution result is 1.2 3.7 4.1 Total value: 9.0 Average: 3.0 It will come out with.
Let's move on to the details.
First, declare the array.
Array declaration
(Variable type name) (Variable name)[] = new (Variable type name)[Number of arrays];
Or
(Variable type name) [](Variablename)=new(Variabletypename)[Number of arrays];
At the very beginning, I declared it in this way.
Excerpt
public static void main(String[] args) {
double[] d = new double[3]; //Declared here. Type of double in variable d,The number of arrays is three.
double sum,avg;
Recommended Posts