Array

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.

Vangie Beal
Vangie Beal
Vangie Beal is a freelance business and technology writer covering Internet technologies and online business since the late '90s.

Related Articles

Canonical

Canonical is the term used to describe an entity that adheres to an original, authoritative text or to a set of rules, principles, and...

Thread

A thread is a sequential, connected, related set of data or content elements found in social media, online messaging, and computer programming settings. What is...

Deprecated

Deprecated or deprecation is the discouragement of the use of a programming language feature. It should not be confused with depreciation, which is the...

Superword Level Parallelism

Superword level parallelism (SLP) is an advanced method of traditional vectorization that facilitates parallelism across loop iterations, SIMD, and basic blocks. Vectorization in this...

ScalaHosting

ScalaHosting is a leading managed hosting provider that offers secure, scalable, and affordable...

HRIS

Human resources information system (HRIS) solutions help businesses manage multiple facets of their...

Best Managed Service Providers...

In today's business world, managed services are more critical than ever. They can...