Arrays
Arrays
Arrays in Java
• An array is a collection of similar types of data.
• Array size is mandatory while creating arrays.
• Array is also known as static data structure because size of an array
must be specified at the time of its declaration.
For example, if we want to store the names of 100 people then we can
create an array of the string type that can store 100 names.
In Java, we can declare and allocate the memory of an array in one single
statement. For example,
How to Initialize Arrays in Java?
In Java, we can initialize arrays during declaration.
For example,
int[] age = {12, 4, 5, 2, 5}; //declare and initialize and array
Here, we have created an array named age and initialized it with the values inside
the curly brackets.
We can also initialize arrays in Java, using the index number. For example,
int[] age = new int[5]; // declare an array
// initialize array
age[0] = 12;
age[1] = 4;
age[2] = 5;
..
How to Access Elements of an Array in Java?
We can access the element of an array using the index number. Here is
the syntax for accessing elements of an array,