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 a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.