What is an Array? And why it's so popular?

ยท

4 min read

What is an Array? And why it's so popular?

What is an Array?

An array is a collection of similar data types.

For example, if we want to store the names of 50 people then we can create an array of string data type that can store 50 names.

String[] names = new String[50];

Here, the above array cannot store more than 50 names. The number of values in a java array is always fixed.


How to Declare an Array in Java?

In Java, here is how we declare an array.

dataType[] arrayName;
  • dataType : It can be primitive data types like int, char, double, boolean, String, etc.
  • arrayName : Its an identifier.

For example,

int[] data ;

Here, data is an array that can hold values of type int.

But how many elements can this array hold?

Good Question !! ๐Ÿ˜ƒ
To define the number of elements that an array can hold, we have to allocate memory for the array in Java. For example,

// declare an array
int[] data;

// allocate memory
data = new int[10];

Here, the array can store 10 elements. We can also say that the length of array is 10.

In Java, we can declare an array and allocate the memory in the same line.
For example,

int[] data = new int[10];

How to initialize Arrays in Java?

In Java, we can initialize arrays during declaration. For example,

// declare and initialize array
int[] age = {20, 24, 18, 9, 13};

Here we have created an array named age and initialized it with the values inside the curly brackets.

We have not provided the size of array above. In this case, Java compiler automatically specifies the size by counting the number of elements.

In array, each memory location is associated with a number. This number is known as array index.
We can also initialize arrays using the index number.
For example,

// declaring array
int[] age = new int[5];

// initialize the array
age[0] = 20;
age[1] = 18;
age[2] = 13;
...

Note :

  • Array indices always start from 0. That is, the first element of an array is at index 0.
  • If the size of an array is n, then the last element of the array will be n-1.

How to Access Elements of an Array?

We can access the element of an array using index number. Here is the syntax for accessing element in an array,

// access array elements
array[index]

Lets see an example of accessing array elements using indexes.

Example : Access Array Elements

class Main{
public static void main(String[] args){

// create an array
int[] age = {20, 18, 13, 17, 6};

// access elements
System.out.println("Accessing Elements of an Array : ");
System.out.println("First Element : " + age[0]);
System.out.println("Second Element : " + age[1]);
System.out.println("Third Element : " + age[2]);
...

Output :

Accessing Elements of an Array :
First Element : 20
Second Element : 18
Third Element : 13
...

In the above example, notice that we use the indexes for accessing the elements.

But we can also use loops for accessing the elements.


Looping Through Array Elements

In Java, we can also loop through each elements of the array. For example,

Example : Using For Loop

class Main{
    public static void main(String[] args){
    // create an array
    int[] age = {4, 7, 14};
    // for loop in array
    System.out.println("Using For Loop : ");
    for (int i = 0; i < arr.length; i++){
        System.out.println(age[i]);
        }
    }
}

Ouput :

Using For Loop :
4
7
14

In the above example, we are using the For Loop to iterate through each element of the array. Notice the expression inside the loop,

age.length

Here we are using age.length property to get the size of array.

We can also use the For-Each Loop to iterate through the elements of an array. For example,

class Main{
    public static void main(String[] args){
    // declare and initialize
    int[] age = {12, 4, 5};
    System.out.println("Using for-each loop : ")
    // for each loop
    for(int a : age){
       System.out.println(a);
       }
    }
}

Output :

Using for-each loop :
12
4
5

Multidimensional Array

Arrays we have mentioned till now are called one-dimensional arrays. However, we can declare multidimensional arrays in Java.

A multidimensional array is an array of arrays. That is each, element of a multidimensional array is an array itself. For example,

int[] matrix = { {4, 7, 9}, {8, 1, 2}, {6, 0, 2} };

Here, we have created a multidimensional array named matrix. Its a 2-dimensional array.


Advantages of Array

  • An Array can store multiple values in a single variable.
  • Arrays are fast as compared to primitive data types.
  • Members of array are stored in consecutive memory locations.

Disadvantages of Array

  • Array has a fixed size.
  • We cannot increase or decrease the size of Array at runtime.
  • Array can store only similar data type items only.
  • Memory wastage can be more.
ย