Array in Java & Types of Array.

So, you know how to store different types of data types? Yes, you already know, by using different variables. But what if we say we have similar sort of data to be stored, do we need to declare different variables for every single piece of data.

Well, the answer is a big NO, because there comes an Array to help us out. So let’s start with …

What is an Array?

An array is a collection of similar types of data at an adjoining memory location.

They can store a fixed number of values under the same name.

What is an Array in Java

Arrays can be super useful in many ways like, collecting the names of students that are of the same class, or list of items you want to pick up from the grocery store.

So now we know what an Array is, so let’s check out how it works.

How does an Array in Java work?

When a program is compiled, it allocates the required memory for the Array in the computer memory.

The amount of memory depends upon the number and the data type of the Array.

The compiler reserves a fixed block of memory for every expected data element in the array.

Once an Array is declared, it can be initialized at any moment by assigning values to it.

Every block of memory within the Array has a unique address that can be used to assign the values to the Array.

The same address can also be used to access the particular block of memory.

But what if we have names and the classes of the students, that is needed to be stored at a single place? What we gonna do?

Well, that’s why we have different types of arrays in java.

Types of Arrays in Java

So basically there are two types of arrays in Java.  

  1. Single Dimensional Array
  2. Multi-Dimensional Array

What are Single Dimensional Arrays?

Single Dimensional Arrays are a linear list of elements of the same type.

Image describing a Single Dimensional Array

These subscripts make the input-output work inside an array really simple.

The different values at different subscripts are called elements of the array where we can store different data based upon the data type of the declared array.

By classifying the element position with the use of these subscripts we can easily store or manipulate the data at any index of any classified array.

How to declare Single Dimensional Array?

Every programming language has a special syntax that we need to follow while writing the code of any coding language.

The same goes with java, there is a special syntax for declaring a single-dimensional array, which we need to take care of.

In order to create a Single Dimensional Array, you first need to declare an array variable of the same type. Let’s see how to do it.

<variable-type> <var-name>[];

Let’s see what this syntax states, here, type declares which type of data array gonna store and var-name is the name given to the variable.

For Example –

int month_days[];

Although this array variable is of no use as there is no physical where the data can be assigned.

To link month_days with a physical array, we first need to allocate one using new and then assign it to month_days.

Let me show you how

int month_days[]= new int[12];

So just when the statement gets executed, the month_days will refer to an array of 12 integers.

Now after you have created an array, it’s time to feed him. So we just need to assign the values directly into its subscript.

An element of an array can be accessed & initialized by specifying it’s subscript within the square brackets.

(Remember, Java index subscript start with zero, so the value [2] is actually for the third month).
int month_days[2]= 31;

Now, to print that particular element, all we need is just the subscript of the element

System.out.println(month_days[2]);

Summarizing all the above points, let’s take a look at the program given below to better understand how it works.

import java.io.*;
 public class month {
     public static void main(String args[]) {
       int month_days[] = new int[12];
       month_days[0] = 31; //here, the values are directly initialized to the array elements using there indexes
       month_days[1] = 28;
       month_days[2] = 31;
       month_days[3] = 30;
       month_days[4] = 31;
       month_days[5] = 30;
       month_days[6] = 31;
       month_days[7] = 31;
       month_days[8] = 30;
       month_days[9] = 31;
       month_days[10] = 30;
       month_days[11] = 31;
System.out.println("Number of days in the month of March = " + month_days[2]+" days."); //here, the value of the index 2 in getting printed
     }
 }

So, just when you run this program, it gonna print the Number of days in the month of March.

You can also initialize the value of elements at the time of its declaration, it won’t change any result. See the program given below.

import java.io.*;
public class MyClass {
    public static void main(String args[]) {
      int month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

      System.out.println("Number of days in the month of March = " + month_days[2]+" days.");
    }
}

How to initialize a single-dimensional Array?

Initializing a single-dimensional array is pretty much easy to do as all it requires the index number where the value is meant to be fed and the value itself.

Here, we will be discussing, how to initialize a variable if we already know the values for the array.

So let’s take a look at the below-given program here,

public class month_days {
    public static void main(String args[]) {
      int month[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //here, we already know the value therefore we have directly initialized the values
      
            System.out.println("Number of days in the month of October is = " + month[9]); //we all know, indexs always starts from 0
    }
}

We have a program telling us the no. of days in every month.

And what if we have to get the data from the user at run time, what we gonna do?

Well, let’s take a look at this program where we are taking 10 names of the students of a class.

What is a Multi-Dimensional Array?

Multi-Dimensional Arrays are actually Arrays of Arrays. These arrays are almost similar to the one we learned about above.

This image contains an image describing how a multi dimesional looks like.

Multi-Dimensional arrays can be very useful in many cases where we need to have multiple pieces of information to store under a single name like a time table or a list of students with their and marks on it.

How to declare a Multi-Dimensional Array?

Declaring a Multi-Dimensional is just like to declaring a Single-Dimensional Array but with an extra pair of square brackets ([ ]) for every dimension of the array.

Let’s take a look at the below-given example –

int two_d[][]= new int[4][5];

And now when the compiler will reach this piece of code, it will create a 4 by 5 array and assign it to the two_d.

How to initialize a Multi-Dimensional Array?

Initializing a Multi-Dimensional Array is similar to initializing a Single-Dimensional Array.

Just like Single-Dimensional Array, it also requires the index number of the places where it needs to be placed.

Now let’s take a look at the below-given program, here we have an array of 4 by 5.

Your Turn!

Now it’s your turn, tell us what do you like about this amazing way to store a vast amount of data under a single name.

Try these programs on your own because as we all know, “Practise makes a man perfect!”.

Thanks for being here!

– Prateek Singh

No votes yet.
Please wait...
I am Prateek Singh and I have been coding and learning The Art of Programming for a while. I have been learning Java and a lot of languages by myself and I thought it would be great to share with those who too want to master this ART. Here, at Programmics, I will be sharing what I learned about this great ART and it will be great to learn together.
Posts created 6

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top
Share via
Copy link