DEV Community

Bharadwaj
Bharadwaj

Posted on

Arrays

Arrays:
Let us understand what is an 'Array' in general.
Collection / group / arrangement of objects is called an Array.

  • Rainbow is an array of colors.
  • An art exhibition has array of paintings.
  • Keyboard has an array of keys.

In Java (or in programming world), the concept of Array is similar. It is a collection of variables having same data type. Imagine this like a box with sequential tiny containers that has a name to it and can store something. (like a Pill Organizer box).

Why we need an Array?
This is a data structure concept. Array allows an easy way to group information that are similar and large is size. Otherwise declaring and storing large amount of details in variable would be time consuming. Complicates the program.

As mentioned earlier, the group of variables within an array should belong to one particular type. For example, each array that is defined shall store either integers (or) strings (or) boolean (or) java datatypes and custom objects. One cannot mix integers and strings in one array.

How to declare an Array?

Syntax: type var-name[];

Explanation:
'type' is the primitive data type followed by 'var-name' name for the variable

For example,

string week_days[];
int month_days[];

The above declaration does not create an Array as such; it only establishes an array variable 'week_days' (or) 'month_days'.

How to create an Array?

Syntax: array-var = new type [size]

Extending the same example given above,

week_days = new String [7];
month_days = new int [31];

Explanation:

array-var - 'week_days' and 'month_days' are the array variables.

new is the operator that allocate memory to create the array.

type is the type of the data being stored in the array

[size] defines the number of elements that can be stored in the array. In above example, 'week_days' array has size of 7 and 'month_days' array has size of 31.

The size of the array is fixed. After creating an array with a particular size, the value cannot be changed.

How to access elements in an Array?
Each element in an array is assigned with an index starting from 0. Index in this context mean a numerical position of the data so that one can refer and access what is inside that index.

For example, in 'week_days' array, the values will be stored in array like

week_days[0] = "Sunday";
week_days[1] = "Monday";
week_days[2] = "Tuesday";
week_days[3] = "Wednesday";
week_days[4] = "Thursday";
week_days[5] = "Friday";
week_days[6] = "Saturday";

The values 0 to 6 is the index. Say, if week_days[4] is called, 'Thursday' would be printed as that value assigned to the index at 4.

7 days a week; yet the index value stops at 6, because the array index starts at 0.

Let us see an example program that covers the above two steps
1) Declare Array variable with a data type
2) Create an array with specific size.

Example Program 01:

package arrays;

public class SimpleArray {

    public static void main(String[] args) {

        //Declaring the variable
        String week_days[];

        //Creating an array
        week_days = new String [7];


        //Assigning value to each index
        week_days[0] = "Sunday";
        week_days[1] = "Monday";
        week_days[2] = "Tuesday";
        week_days[3] = "Wednesday";
        week_days[4] = "Thursday";
        week_days[5] = "Friday";
        week_days[6] = "Saturday";

        System.out.println("First day of the work week is " + week_days[1]);
        }

}

Enter fullscreen mode Exit fullscreen mode

Output of Example Program 01:
First day of the work week is Monday

Is there any other way to declare and create an Array? Yes

Declaration of Array Variable and creating an Array can be done in one statement.

Example:
//Declaring the variable
String week_days[];

//Creating an array
week_days = new String [7];

This can be rewritten as

String week_days [] = new String [7];

Arrays can also be initialized along declaration without the use of 'new' keyword. This is an alternate way to create and initialize arrays.

In same week_days example, the array can be created like

String week_days [] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}

Note the usage of curly braces { } and 'new' keyword is not used.

Example Program 02:

package arrays;

public class ArrayCreation {

    public static void main(String[] args) {
        // Declare and initialize array
        String week_days [] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
        System.out.println("First day of the work week is " + week_days[1]);


    }

}

Enter fullscreen mode Exit fullscreen mode

Output of Example Program 02:
First day of the work week is Monday

Both programs display the same output; yet the way arrays are declared and created differs.

More programs on Arrays, Array dimension to follow in upcoming posts.

Top comments (1)

Collapse
 
payilagam_135383b867ea296 profile image
Payilagam

Nice narration!