Array in Java
Array in Java
Introduction
Arrays allow you to store multiple items of the same type together, making it easier to manage and
manipulate data.
An Array contains a limited amount of numbered spots (indices) for values. The length (or size) of an
Array is the amount of these spots, i.e. how many values can you place in the Array. The values in an
Array are called elements.
Arrays are used to store multiple values in a single variable, instead of declaring separate variables
for each value.
To declare an array, define the variable type with square brackets:
Arrays in Java are one of the most fundamental data structures that allow us to store multiple values
of the same type in a single variable. They are useful for storing and managing collections of data.
Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of
memory management. For primitive arrays, elements are stored in a contiguous memory location,
for non-primitive arrays, references are stored at contiguous locations, but the actual objects may
be at different locations in memory.
Key features of Arrays:
Contiguous Memory Allocation (for Primitives): Java array elements are stored in
continuous memory locations, which means that the elements are placed next to each other
in memory.
Zero-based Indexing: The first element of the array is at index 0.
Fixed Length: Once an array is created, its size is fixed and cannot be changed.
Can Store Primitives & Objects: Java arrays can hold both primitive types (like int, char,
boolean, etc.) and objects (like String, Integer, etc.)
This is an array of seven elements. All the elements are integers and homogeneous. The green box
below the array is called the index, which always starts from zero and goes up to n-1 elements. In this
case, as there are seven elements, the index is from zero to six.
There are three main features of an array:
1. Dynamic allocation: In arrays, the memory is created dynamically, which reduces the amount
of storage required for the code.
2. Elements stored under a single name: All the elements are stored under one name. This
name is used any time we use an array.
3. Occupies contiguous location: The elements in the arrays are stored at adjacent positions.
This makes it easy for the user to find the locations of its elements.
Here, the type is int, String, double, or long. Var-name is the variable name of the array.
We have declared an array arr of type integer. The size of the array is 5, meaning that it can have five
elements. The array is assigned with elements for each of the index positions. We'll run a for loop to
print the elements in the array. A counter variable "i" is used to increment the index position after
checking if the current index position is less than the length of the array.
Types of Arrays
There are three types of arrays. We use these types of arrays as per the requirement of the program.
These are:
One-dimensional Array
Also known as a linear array, the elements are stored in a single row. For example:
Two-dimensional Array
Two-dimensional arrays store the data in rows and columns:
In this, the array has two rows and five columns. The index starts from 0,0 in the left-upper corner to
1,4 in the right lower corner.
Multi-dimensional Array
This is a combination of two or more arrays or nested arrays. We can even use more than two rows
and columns using the following code:
Here, we are using three rows and three columns, but we are only using two for loops. Regardless of
how many rows and columns are entered, the number of for loops will always be two.
1. Array Declaration
To declare an array in Java, use the following syntax:
type[] arrayName;
type: The data type of the array elements (e.g., int, String).
arrayName: The name of the array.
Note: The array is not yet initialized.
2. Create an Array
To create an array, you need to allocate memory for it using the new keyword:
// Creating an array of 5 integers
int[] numbers = new int[5];
This statement initializes the numbers array to hold 5 integers. The default value for each element
is 0.
5. Array Length
We can get the length of an array using the length property:
// Getting the length of the array
int length = numbers.length;