Array in java

In java, we store some variables to do some processing. but sometimes we have to declare so many variables with the same datatype

for example :

you have to enter 100 different numbers into different variables to do some processing.
but It takes a lot of time to declare 100 variable and initialization them

For this, there is an easier and efficient way to store the variable.
java provides array

What is an array:

The  array is a collection of similar data type or it is a data structure where we store similar elements in continue memory location

Feature of the array:

  • The element of the array are store in a continues memory location
  • Array store the elements of the same datatype
  • In array, we can only store fix no of element
  • Array in java is index-based

Memory and index diagram or array:

Memory and index diagram or array:

Java Array INDEX:

In Java, each element in an array is associate with a number in the memory location. The number is known as an array index. We can access elements of an array by using those indexes. For example,

int[] age = new int[5];
java array index

How to declare array :

dataType[] arr; (or)  
dataType []arr; (or)  
dataType arr[];  
  • in the square bracket, we define the size of the array
  • the datatype can be primitive like int, char, double, byte, etc.

Instantiation of an Array in Java:

arrayVar=new datatype[size];

Note: we cannot change the size of the array after the declaration

Example of the array (declaration, Instantiation, and Initialization):

//Java Program to illustrate the use of declaration, instantiation   
//and initialization of Java array in a single line  
class ArrayExample{  
public static void main(String args[]){  
int a[]={45,11,5,6,8};//declaration, instantiation and initialization  
//printing array  
for(int i=0;i<a.length;i++)//length is the property of array  
System.out.println(a[i]);  
}
}  
45
11
5
6
8

Note:  if we don’t initialize the array. Then default value will store in it

Example:

class ArrayExample {
    public static void main(String[] args) {
    
        // create an array of length 5  
        int[] age = new int[5];
     
        // access each element of the array using the index number 
        System.out.println(age[0]);
        System.out.println(age[1]);
        System.out.println(age[2]);
        System.out.println(age[3]);
        System.out.println(age[4]);
    }
}
0
0
0
0
0

Type of array:

Types of array

In this article we discuss single dimensional array

Single Dimensional Array

in the previous topic, we learn about array. In this article, we will learn about single Dimensional array

Single Dimensional array:

Single Dimensional array are those array which store elements in a  single row

For example

This is a block of memory locations on the computer. Where we declare a single-dimension array

int[] age = {41, 16, 28, 19, 15, 18};

in  this age is the reference variable which points to a continues memory location

single dimension array

Example of a single-dimensional array :

Java Program to print the array elements using for-each loop 

class Testarray1{  
public static void main(String args[]){  
int arr[]={33,3,4,5};  
//printing array using for-each loop  
for(int i:arr)  
System.out.println(i);  
}
}  
33
3
4
5


SHARE THIS PAGE





Important Link