---
title: "Arrays"  
description: "An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created."  
author: "ANkita gupta"  
published: 2015-05-07  
updated: 2018-02-21  
canonical: https://www.mindstick.com/blog/10882/arrays  
category: "java"  
tags: ["java"]  
reading_time: 4 minutes  

---

# Arrays

An array is a [container](https://www.mindstick.com/forum/159610/how-to-connect-a-windows-container-to-a-service-running-on-localhost) object that holds a fixed number of values of a single type. The [length of an array](https://answers.mindstick.com/qa/104684/how-to-find-the-length-of-an-array) is established when the array is created. After [creation](https://yourviews.mindstick.com/view/85506/what-was-the-causes-of-creation-of-pakistan), its length is fixed. You have seen an example of arrays already, in the main method of the "[Hello World](https://www.mindstick.com/forum/12912/how-to-show-hello-world-backbone-marionette-js)!" application. This section discusses arrays in greater detail.

![Illustration of an array as 10 boxes numbered 0 through 9; an index of 0 indicates the first element in the array](https://docs.oracle.com/javase/tutorial/figures/java/objects-tenElementArray.gif)

An array of 10 elements.

\

Each item in an array is called an element, and each element is accessed by its numerical index. As shown in the preceding illustration, numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8.

The following program, [ArrayDemo](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/examples/ArrayDemo.java), creates an [array of integers](https://www.mindstick.com/forum/157932/write-a-program-to-find-the-missing-number-in-an-array-of-integers), puts some values in the array, and prints each value to [standard](https://www.mindstick.com/articles/23223/naming-convention-or-coding-standard) output.

```
class ArrayDemo {
    public static void main(String[] args) {
        // declares an array of integers
        int[] anArray;

        // allocates memory for 10 integers
        anArray = new int[10];

        // initialize first element
        anArray[0] = 100;
        // initialize second element
        anArray[1] = 200;
        // and so forth
        anArray[2] = 300;
        anArray[3] = 400;
        anArray[4] = 500;
        anArray[5] = 600;
        anArray[6] = 700;
        anArray[7] = 800;
        anArray[8] = 900;
        anArray[9] = 1000;

        System.out.println("Element at index 0: "
                           + anArray[0]);
        System.out.println("Element at index 1: "
                           + anArray[1]);
        System.out.println("Element at index 2: "
                           + anArray[2]);
        System.out.println("Element at index 3: "
                           + anArray[3]);
        System.out.println("Element at index 4: "
                           + anArray[4]);
        System.out.println("Element at index 5: "
                           + anArray[5]);
        System.out.println("Element at index 6: "
                           + anArray[6]);
        System.out.println("Element at index 7: "
                           + anArray[7]);
        System.out.println("Element at index 8: "
                           + anArray[8]);
        System.out.println("Element at index 9: "
                           + anArray[9]);
    }
} 
```

The output from this program is:

```
Element at index 0: 100
Element at index 1: 200
Element at index 2: 300
Element at index 3: 400
Element at index 4: 500
Element at index 5: 600
Element at index 6: 700
Element at index 7: 800
Element at index 8: 900
Element at index 9: 1000
```

In a real-world [programming](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming) situation, you would probably use one of the supported looping constructs to iterate through each [element of the array](https://www.mindstick.com/forum/158053/how-to-remove-the-matching-element-of-the-array-in-javascript), rather than write each line individually as in the preceding example. However, the example clearly illustrates the array syntax. You will learn about the various looping constructs (for, while, and do-while) in the [Control flow](https://www.mindstick.com/interview/33772/what-is-the-control-flow-function-and-its-execution-in-control-flow-statements) section.

\

---

Original Source: https://www.mindstick.com/blog/10882/arrays

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
