Java Array - ashish9342/FreeCodeCamp GitHub Wiki
An Array is used to store a collection of data of similar datatype. Arrays always start with the index of 0.
Syntax:
dataType[] name_of_array; // preferred way.
or
dataType name_of_array[]; // works but not preferred way
double[] list; //preferred way.
or
double list[]; //works but not preferred way.
Note: The style double list[]
is not preferred as it comes from the C/C++ language and was adopted in Java to accommodate C/C++ programmers.
dataType[] name_of_array = new dataType[arraySize];
double[] List = new double[10];
dataType[] name_of_array = {value0, value1, ..., valuek};
double[] list = {1, 2, 3, 4};
Example of code:
int[] a = new int[] {4,5,6,7,8}; //declare array
for (int i=0; i<a.length; i++) //loop goes through each index
{
System.out.println(a[i]); //prints the array
}
🚀 Run Code
Output:
4
5
6
7
8
Source: Java Arrays