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.

Top Articles

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...

What are the Five Generations of Computers? (1st to 5th)

Reviewed by Web Webster Each generation of computer has brought significant advances in speed and power to computing tasks. Learn about each of the...

Hotmail [Outlook] Email Accounts

Launched in 1996, Hotmail was one of the first public webmail services that could be accessed from any web browser. At its peak in...

ITSM Tools

IT Service Management (ITSM) tools are the tools involved in designing, delivering, and...

Unified Communications as a...

Unified communications as a service (UCaaS) is a cloud-based communications solution that provides...

MDM vs. MAM

The bring your own device (BYOD) phenomenon has changed how businesses interact with...