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 is a freelance business and technology writer covering Internet technologies and online business since the late '90s.

Top Articles

The Complete List of 1500+ Common Text Abbreviations & Acronyms

From A3 to ZZZ we list 1,559 SMS, online chat, and text abbreviations to help you translate and understand today's texting lingo. Includes Top...

List of Windows Operating System Versions & History [In Order]

The Windows operating system (Windows OS) refers to a family of operating systems developed by Microsoft Corporation. We look at the history of Windows...

How to Create a Website Shortcut on Your Desktop

Website Shortcut on Your Desktop reviewed by Web Webster   This Webopedia guide will show you how to create a website shortcut on your desktop using...

Generations of Computers (1st to 5th)

Reviewed by Web Webster Learn about each of the 5 generations of computers and major technology developments that have led to the computing devices that...

Snail Mail

Snail mail, also called direct mail, is a slang term for...

CC vs BCC: What...

CC and BCC are two options to add third-party recipients to...

Eventbrite

  Eventbrite is an online event posting, event management, and ticketing website. Eventbrite can...