Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

WHAT IS AN ARRAY?

ARRAY

An array is a collection of items of same data type stored at


contiguous memory locations. In Java, an array is a data structure that
allows you to store a fixed-size, ordered collection of elements of the same
data type. Each element in an array is accessible by its index, starting from
0 for the first element. Arrays are commonly used to store and manipulate
lists of data.
ARRAY

An array is a way to reference a series of memory locations using


the same name. Each memory location is represented by an array element.
An array element is similar to one variable except it is identified by an
index value instead of a name. An index value is a number used to identify
an array element.
WHY AN ARRAY?

There are differences between an array element and a variable,


and those differences make working with large amounts of data a breeze.
The main difference between an array and a variable is that all the array
elements are next to each other in memory. Variables can be anywhere in
memory. The name of a variable references one memory location. The
name of an array references one or multiple memory locations when
combined with an index.
ARRAYS AND DATA STRUCTURES

The importance of using arrays for data structures is that you can
easily change the order of data by using pointers and pointers to pointers
without having to touch the original data. In the real world, pointers
typically point to a whole group of information such as a client s name,
address, phone number, and other pertinent data.
DECLARING AN ARRAY
The way to declare an array depends on the programming
language used to write your program. In Java, there are two techniques
for declaring an array. You can declare and initialize an array either
where memory is allocated at compile time or where memory is
dynamically allocated at runtime. Allocation is another way of saying
reserving memory.
Let’s begin by declaring an array where memory is reserved when
you compile your program. This technique is similar in Java, C, and C++,
except in Java you must initialize the array when the array is declared.
There are components of a statement that declares an array. These
components are a data type, an array name and the total number of
array element to create.

C++: int grades[10];

Java: int grades[] = new int[10];


In Java, you must initialize the array when the array is declared as
shown here. The size of the array is automatically determined by counting
the number of values within the braces. Therefore, there isn’t any need to
place the size of the array within the square brackets:

int grades[] = new int[10];

int grades[] = { 1, 0, 1, 0, 1, 0, 1, 1 };

int grades[] = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };


The data type is a keyword that tells the computer the amount of
memory to reserve for each element of the array. In this example, the
computer is told to reserve enough memory to store an integer for each
array element.

The array name is the name you use within a program to reference
an array element. The array name in this example is grades . The number
within the square brackets is the total number of elements that will be in the
array.
In order to allocate memory at compile time, you must know the
number of array elements that you need. Sometimes you don’t know this,
especially if your program loads the array with data stored in a
database. The amount of data stored in a database typically fluctuates.

The solution in Java is to allocate memory at runtime. Programmers


call this dynamically allocating memory. You dynamically allocate memory
by using the new operator when declaring the array, as shown here:

int grades[] = new int[10];


MULTIDIMENSIONAL ARRAYS
A multidimensional array consists of two or more arrays defined by
sets of array elements. Each set of array elements is an array. The first set
of array elements is considered the primary array, and the second and
subsequent sets of array elements are considered subarrays.

A multidimensional array can be useful to organize subgroups of


data within an array. Let’s say that a student has three grades, a mid-term
grade, a final exam grade, and a final grade. You can store all three
grades for an endless number of students in a two-dimensional array.
MULTIDIMENSIONAL ARRAY IN MEMORY

Data stored in a multidimensional array is stored sequentially by sets


of elements. The name of a multidimensional array references the memory
address of the first element of the first set of elements. You can use the
name of a multidimensional array as a pointer to the entire array. The
index of the first element of the first set of array elements points to the
memory address where values assigned to array elements are stored.
DECLARING MULTIDIMENSIONAL ARRAY

A multidimensional array is declared similar to the way you declare


a one-dimensional array except you specify the number of elements in both
dimensions. For example:

C++: int grades[3][4];

Java: int grades[][] = new int[3][4];


In this example, all the arrays pointed to by the first index are of the
same size. The second index can be of variable size. For example, the
previous statement declares a two-dimensional array where there are 3
elements in the first dimension and 4 elements in the second dimension.

The data type tells the computer that each element of the array will
contain an integer data type. The data type is followed by the array name
and two values that indicate the size of each dimension used for the array.
In this case, there are three sets of four array elements.
ASSIGNING VALUES TO A MULTIDIMENSIONAL ARRAY

You assign a value to an element of a multidimensional array with an


assignment statement similar to the assignment statement that assigns a
value to a single-dimensional array, as shown here:

grades[0][0] = 1001;

You must specify the index for both dimensions. In this example, the integer
1001, which could be a student ID, is assigned to the first element of the
first set of elements in the grades array.
SUPPLEMENTARY LEARNING RESOURCES / REFERENCES
What is array? (2022, November 30). GeeksforGeeks. Retrieved August 31, 2023, from
https://www.geeksforgeeks.org/what-is-array/
Arrays in Java. (2023, January 23). GeeksforGeeks. Retrieved August 31, 2023, from
https://www.geeksforgeeks.org/arrays-in-java/

You might also like