Java Array


The array is a collection of data that store the fixed number of values of the same type.

Syntax:-

datatype[] array_Name;
or
datatype array_Name[]; 

Example:

if we want to store marks of 100 students, then we can create an array for it.

 float[] marks;

Size and type of arrays cannot change after it's declared.

Two types of Arrays:

  • One-dimensional arrays
  • Multidimensional(2D) arrays
Java Array

Array Type Description
Single Dimensional array In this we create and initialize all the values in the single array.
Multi-Dimensional Array Java supports the multi-dimensional array. The multi-dimensional array also called as the Two-Dimensional array.

Advantages of an array:

  • It is better to store the data of the same data type with the same size.
  • Coding is less, one variable can store a number of values.
  • It allocates memory in different memory locations for its elements. There is no memory flow or shortage of memory in arrays.
  • Iterating is faster in arrays compared to other methods like a linked list.
  • By using array data retrieving is easy.
  • Easily short the data using the swapping method.
  • By array index, we can randomly access any element from the array.
  • It supports multidimensional array.


Array Declaration in Java

To declare an array in Java programming language.

Syntax:-

 dataType[] arrayReferencevariable = new dataType[arraySize];

Array Initialization in Java

An array is initialized by using the index of each element.

Syntax:-

int[] score = {100, 221, 59, 54};

Java Array initialize


Example:-

 public class Array 
{  
    public static void main(String []args) {      
		int[] score = {100, 221, 59, 54};   
		for(int i=0; i < score.length; i++)
		{        
        	System.out.println(" Element["+i+"]= "+score[i]);  
        } 
    }
}



Onlinetpoint is optimized for basic learning, practice and more. Examples are well checked and working examples available on this website but we can't give assurity for 100% correctness of all the content. This site under copyright content belongs to Onlinetpoint. You agree to have read and accepted our terms of use, cookie and privacy policy.