---
title: "Array in Java"  
description: "Array is a collection similar data type.  Java provides a data structure, the array, which stores a fixed-size sequential collection of elements of th"  
author: "Manoj Pandey"  
published: 2015-04-27  
updated: 2018-02-23  
canonical: https://www.mindstick.com/blog/10874/array-in-java  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Array in Java

The [array](https://www.mindstick.com/articles/335/jagged-array-in-c-sharp-dot-net) is a [collection](https://www.mindstick.com/articles/1718/collections-in-java) similar data type. Java provides a [data structure](https://www.mindstick.com/blog/11221/simple-way-to-learn-dynamic-data-structure-in-c-language), the array, which [stores](https://www.mindstick.com/forum/161350/how-does-sqlite-database-stores-tables) a fixed-size [sequential](https://www.mindstick.com/interview/23447/what-are-the-different-methods-for-sequential-supervised-learning) collection of [elements](https://www.mindstick.com/forum/1440/wpf-button-with-multiple-text-elements) of the same type. An array is used to [store](https://www.mindstick.com/articles/13125/tips-to-increase-sales-of-your-woocommerce-store) a collection of data, but it is often more [useful](https://www.mindstick.com/articles/12694/how-dolomite-mineral-is-useful-in-human-life) to think of an array as a collection of [variables](https://www.mindstick.com/articles/715/php-variables) of the same type.

\

![Array in Java](https://www.mindstick.com/blogs/924d8d4f-c09c-4524-a039-aaa8c7311718/images/0acb1615-1922-4486-ba76-e52586a94382.png)

\

**Creating Arrays:**

```
dataType[] arrayRefVar;arrayRefVar = new dataType[arraySize];or dataType[] arrayRefVar = {value0, value1, ..., valuen};Example -:   double[ ] myList = new double[10];
```

**\**

**Types of Array in java**

There are two types of array.

- Single Dimensional Array
- Multidimensional Array

Example of Single Dimensional array

```
class Testarray{  public static void main(String args[]){    int a[]=new int[5];//declaration and instantiation  a[0]=10;//initialization  a[1]=20;  a[2]=70;  a[3]=40;  a[4]=50;    //printing array  for(int i=0;i<a.length;i++)//length is the property of array  System.out.print(a[i]+ " ");    }
```

Output-: 10 20 70 40 50

**Multidimensional array in java**

Declare of multidimensional array: dataType[][] arrayRefVar; (or)

```
dataType [][]arrayRefVar; (or)  dataType arrayRefVar[][]; (or)  dataType []arrayRefVar[];Example-:  int[][] arr=new int[3][3];  //3 row and 3 column  Initialized multidimensional arrayarr[0][0]=1;  arr[0][1]=2;  arr[0][2]=3;  arr[1][0]=4;  arr[1][1]=5;  arr[1][2]=6;  arr[2][0]=7;  arr[2][1]=8;  arr[2][2]=9;
```

Example of multidimensional array

```
class Sample {   public static void main(String args[]) {      //declaring and initializing 2D array        int arr[][]={{1,2,3},{2,4,5},{4,4,5}};          //printing 2D array        for(int i=0;i<3;i++){           for(int j=0;j<3;j++){           System.out.print(arr[i][j]+" ");        }        System.out.println();     } }
```

\

Example of the array using for loop

```
public class Sample {    public static void main(String[] args) {      double[] myList = {1.9, 2.9, 3.4, 3.5};       // Print all the array elements      for (double element: myList) {         System.out.println(element);      }   }}
```

Output-:

Advantage of Java Array\
Disadvantage of Java Array

1.9

2.9

3.4

3.5

\

**Code Optimization:** It makes the code optimized, we can retrieve or sort the data easily.

\

**Random access:** We can get any data located at any index position.

\

**Size Limit:** We can store the only fixed size of elements in the array. It doesn't grow its size at runtime. To solve this problem, collection framework is used in java.

---

Original Source: https://www.mindstick.com/blog/10874/array-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
