One Dimensional Array in Java – 1D Array

Here, we will discuss what is a one-dimensional array and how to use it in Java.

A one-dimensional array or 1D array is the list of variables of the same data type stored in the contiguous memory locations. We can access these variables of a 1-d array by an index value in square brackets followed by name of the array.

So to declare the array we need the datatype of the values which we want to store, array name, and the array size that is how many elements the user needs to store.

The data type can be pre-defined (int, char, double, byte, etc.) or User-defined data (Objects).

Declaration of an Array

In this, we have to specify the data type according to our requirements. We also have to write the name of the array so that we can use it later using its name.


Instantiating of an Array

For the creation of an array, we have to use a new keyword with the data type of an array. It is necessary to mention the size of the array. The size must be an integer value or variable containing an integer value. The number of elements you can store in an array totally depends upon its size.



Program for Creating an Array of Integer Type:


How Array Implemented in Memory?

In the above program, we created a class named ‘Array’. In the main function, we created an array of integer types. An integer used 4 bytes, every element of an array contains a 4-byte block in memory and the overall size of an array is 16 bytes. ‘a’ is a reference variable, which points to the base address (first element address) of array elements. The first element of an array is stored at the 0th index, the 2nd element is stored at the 1st index, and so on.

I will define this implementation using a diagram.

1-D-ArrayYou can see that in this pictorial representation, let base address(first element address) starting from 100 and we know that integer contains 4 bytes so the next element’s address is 104 and so on.

Advantages of 1D Array

  • We can access random elements of an array using its indexes.
  • With the help of an array, we can store many elements at a time.
  • In an array, we don’t need to declare multiple reference variables for storing multiple values. Only one reference variable represents the whole array.
  • The location of elements in an array is too easy.
  • A very important disadvantage of the primitive datatype that they cannot store values in the continuous location is solved using an array.

Disadvantages of 1D Array

  • We cannot store different data – types of values.
  • We have to define array size at the declaration time.
  • It is not possible to increase or decrease the size of an array.
  • If we initialize an array with the given size and we are not using the whole memory of an array so, this is the wastage of the memory. And, It is the biggest limitation of an array.

So, that’s all about 1D Array in Java. Comment down below if you have any queries.

Leave a Comment

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