Home / Definitions / Array

Array

Vangie Beal
Last Updated September 27, 2021 9:28 am

An array is a data structure in computer programming that organizes data. Arrays hold elements with the same data type, such as integers, characters, or strings

What does an array do?

Arrays organize data of the same type in an application. Each element in an array is indexed numerically, which makes locating data within a program orderly and straightforward. Usually, indexes start with the number 0, so that the first element is numbered 0, the second numbered 1, etc. This is called zero-based indexing. 

An array appears like a bracketed list. Elements are grouped together in the brackets, ordered by their index number. The following instructions will use Java as the primary example in this definition. 

How to declare an array

Each Java array needs two things: a type and a name. If the elements within an array are characters, the array type is char [  ] . The programmer will designate the array’s name: for this definition, we’ll use ArrayName. 

Declaring an array in Java looks like this:

char [ ] ArrayName

or

char ArrayName [ ]

How to initialize elements and add values in an array

After an array is declared, it needs to be created, or given value:

ArrayName = new char [10]

10 is the number of indexed elements in the array.

To initialize each element, give each element value:

ArrayName [0] = "A"

The number in brackets—0—is the index number in the array (the first element); A is the character value for the first element. Character types must be surrounded by quotes to add value, but integers do not.

To initialize an integer, give each integer a value:

ArrayName [0] = 11

To initialize the first string in an array:

ArrayName [0] = {Java}

In Java, traditional arrays cannot be resized, but ArrayLists can. Java ArrayLists are classes that allow programmers to manipulate arrays. 

Read more: How to Create a Java ArrayList Class

 

This article was updated Sept. 2021 by Jenna Phipps.