Tutorial: One Dimensional Array in Java

Now let’s study the structure of Arrays in Java. Array is the most widely used data structure in Java. It can contain multiple values of the same type. Moreover, arrays are always of fixed length i.e. the length of an array cannot be increased or decreased.

Let’s have a close look over the structure of Array. Array contains the values which are implicitly referenced through the index values. So to access the stored values in an array we use indexes. Suppose an array contains “n” integers. The first element of this array will be indexed with the “0” value and the last integer will be referenced by “n-1” indexed value.

Presume an array that contains 6 elements as shown in the figure. Each element is holding a distinct value. Here the first element is referenced by a[0] i.e. the first  index value.

We have filled the 6 distinct values in the array each referenced as:

a[0] = 1

a[1] = 2

a[2] = 3

a[3] = 4

a[4] = 5

a[5] = 6

…here n = 6, therefore n-1 is 5 where we have stored 6 which is the value of n.

Diagrammatically

a[0]

a[1]

a[2]

a[3]

a[4]

a[5]

1

2

3

4

5

6

One Dimensional Array

Array variable has a type and a valid Java identifier i.e. the array’s type and the array’s name. By type we mean the type of elements contained in an array. To represent the variable as an Array, we use [] notation. These two brackets are used to hold the array of a variable.

How to Declare an Array in Java

By array’s name, we mean that we can give any name to the array, however it should follow the predefined conventions. Below are the examples which show how to declare an array –

int days[];      //declares an array of integers

int[] years;    //declares an array of integers

String months[];   //declares an array of Strings

double salaries[];  //declares an array of doubles

It is essential to assign memory to an array when we declare it. Memory is assigned to set the size of the declared array. The “new” operator is used for the allocation of memory to the array object. Here, the new operator is followed by the type of variable and the number of elements to be allocated. In this example [ ] operator has been used to place the number of elements to be allocated.

For example:

1.

int days[] = new int[31];  //single step

this can be done in two steps also:

int days[];

days = new int[31];

2.

String []months = new String[12];        //single step

This can be done in two steps also:

String []months;

months = new String[12];

Java Program

class ArrayDemo{
  public static void main(String args[]){
    int count = 0;
    int days[] = new int[31];
    while(count<days.length){
      days[count] = ++count;
    }
    for(int index = 0;index<days.length;index++){
      System.out.println("days["+index+"] = "+days[index]);
    }
  }
}

Output

program for one dimensional array in Java

Explanation of Code & Output

In this example, a variable ‘days’ is declared which has a type array of int, that is, int[]. The variable days is initialized to reference a newly created array object. The expression ‘int[] = new int[50]’ specifies that the array should have 50 components. To know the length of the Array, we use field length, as shown.

In our next tutorial, we will discuss about how to use multi dimensional arrays in Java.

Checkout more useful tutorials and definitive guidelines on Java programming here.

About the author

Nitin Agarwal

A blogger, tech evangelist, YouTube creator, books lover, traveler, thinker, and believer of minimalist lifestyle.

1 Comment

Click here to post a comment