Unit-3
Arrays:
Arrays are fundamental structures in Java that allow us to store multiple values of the same
type in a single variable. They are useful for managing collections of data efficiently. Arrays in
Java work differently than they do in C/C++. This article covers the basics and in-
depth explanations with examples of array declaration, creation, and manipulation in Java.
Table of Content
Basics of Arrays in Java
In-depth Concepts of Java Array
Arrays in Java
Creating, Initializing, and Accessing an Arrays in Java
Instantiating an Array in Java
Array Literal in Java
Types of Arrays in Java
o 1. Single-Dimensional Arrays
o 2. Multi-Dimensional Arrays
Arrays of Objects in Java
Multidimensional Arrays in Java
Passing Arrays to Methods
Returning Arrays from Methods
Class Objects for Arrays
Java Array Members
Cloning of Single-Dimensional Array in Java
Cloning Multidimensional Array in Java
Advantages of Java Arrays
Disadvantages of Java Arrays
Basics of Arrays in Java
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.
Array Declaration Example:
Here’s an example of declaring an integer array:
int[] numbers; // Declaring an integer array
, This statement declares an array named numbers that will hold integers. Note: The array
is not yet initialized.
Create an Array
To create an array, you need to allocate memory for it using the new keyword:
numbers = new int[5]; // Creating an array of 5 integers
This statement initializes the numbers array to hold 5 integers. The default value for
each element is 0.
Access an Element of an Array
We can access array elements using their index, which starts from 0:
numbers[0] = 10; // Setting the first element of the array
int firstElement = numbers[0]; // Accessing the first element
The first line sets the value of the first element to 10. The second line retrieves the value
of the first element.
Change an Array Element
To change an element, assign a new value to a specific index:
numbers[0] = 20; // Changing the first element to 20
This statement updates the value of the first element from 10 to 20.
Array Length
We can get the length of an array using the length property:
int length = numbers.length; // Getting the length of the array
This retrieves the number of elements in the numbers array, which is 5 in this case.
The above are the basic details of Arrays in Java. Now, if you want to go through the in-depth
concepts of Java Arrays, then scroll down below and go through the diagrams, examples, and
explanations.
In-depth Concepts of Java Array
Following are some important points about Java arrays.
Arrays in Java
In Java, all arrays are dynamically allocated.
Arrays may be stored in contiguous memory [consecutive memory locations].
Since arrays are objects in Java, we can find their length using the object
property length. This is different from C/C++, where we find length using size of.
A Java array variable can also be declared like other variables with [] after the data type.
The variables in the array are ordered, and each has an index beginning with 0.
Java array can also be used as a static field, a local variable, or a method parameter.
An array can contain primitives (int, char, etc.) and object (or non-primitive) references of a
class, depending on the definition of the array. In the case of primitive data types, the actual
values might be stored in contiguous memory locations (JVM does not guarantee this behavior).
,In the case of class objects, the actual objects are stored in a heap segment. To learn more
about Java Array, go through Java programming course here.
Note: This storage of arrays helps us randomly access the elements of an array [Support
Random Access].
Creating, Initializing, and Accessing an Arrays in Java
The general form of array declaration is
-- type var-name[];
-- type[] var-name;
An array declaration has two components: the type and the name.
type declares the element type of the array.
The element type determines the data type of each element that comprises the array.
Like an array of integers, we can also create an array of other primitive data types like
char, float, double, etc., or user-defined data types (objects of a class).
Thus, the element type for the array determines what type of data the array will hold.
Example:
// both are valid declarations
int intArray[];
int[] intArray;
// similar to int we can declare
// byte , short, boolean, long, float
// double, char
// an array of references to objects of
// the class MyClass (a class created by user)
MyClass myClassArray[];
// array of Object
Object[] ao,
// array of Collection
// of unknown type
Collection[] ca;
, Although the first declaration establishes that int Array is an array variable, no actual
array exists.
It merely tells the compiler that this variable (int Array) will hold an array of the integer
type.
To link int Array with an actual, physical array of integers, you must allocate one
using new and assign it to int Array.
Instantiating an Array in Java
When an array is declared, only a reference of an array is created. To create or give memory to
the array, you create an array like this: The general form of new as it applies to one-dimensional
arrays appears as follows:
var-name = new type [size];
Here, type specifies the type of data being allocated, size determines the number of elements in
the array, and var-name is the name of the array variable that is linked to the array. To
use new to allocate an array, you must specify the type and number of elements to allocate.
Example:
//declaring array
int intArray[];
// allocating memory to array
intArray = new int[20];
// combining both statements in one
int[] intArray = new int[20];
Note: The elements in the array allocated by new will automatically be initialized to zero (for
numeric types), false (for boolean), or null (for reference types). Do refer to default array values
in Java.
Obtaining an array is a two-step process. First, you must declare a variable of the desired array
type. Second, you must allocate the memory to hold the array, using new, and assign it to the
array variable. Thus, in Java, all arrays are dynamically allocated.
Array Literal in Java
In a situation where the size of the array and variables of the array are already known, array
literals can be used.
// Declaring array literal
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 };
The length of this array determines the length of the created array.
There is no need to write the new int[] part in the latest versions of Java.
Accessing Java Array Elements using for Loop
Each element in the array is accessed via its index. The index begins with 0 and ends at (total
array size)-1. All the elements of array can be accessed using Java for Loop.
Arrays:
Arrays are fundamental structures in Java that allow us to store multiple values of the same
type in a single variable. They are useful for managing collections of data efficiently. Arrays in
Java work differently than they do in C/C++. This article covers the basics and in-
depth explanations with examples of array declaration, creation, and manipulation in Java.
Table of Content
Basics of Arrays in Java
In-depth Concepts of Java Array
Arrays in Java
Creating, Initializing, and Accessing an Arrays in Java
Instantiating an Array in Java
Array Literal in Java
Types of Arrays in Java
o 1. Single-Dimensional Arrays
o 2. Multi-Dimensional Arrays
Arrays of Objects in Java
Multidimensional Arrays in Java
Passing Arrays to Methods
Returning Arrays from Methods
Class Objects for Arrays
Java Array Members
Cloning of Single-Dimensional Array in Java
Cloning Multidimensional Array in Java
Advantages of Java Arrays
Disadvantages of Java Arrays
Basics of Arrays in Java
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.
Array Declaration Example:
Here’s an example of declaring an integer array:
int[] numbers; // Declaring an integer array
, This statement declares an array named numbers that will hold integers. Note: The array
is not yet initialized.
Create an Array
To create an array, you need to allocate memory for it using the new keyword:
numbers = new int[5]; // Creating an array of 5 integers
This statement initializes the numbers array to hold 5 integers. The default value for
each element is 0.
Access an Element of an Array
We can access array elements using their index, which starts from 0:
numbers[0] = 10; // Setting the first element of the array
int firstElement = numbers[0]; // Accessing the first element
The first line sets the value of the first element to 10. The second line retrieves the value
of the first element.
Change an Array Element
To change an element, assign a new value to a specific index:
numbers[0] = 20; // Changing the first element to 20
This statement updates the value of the first element from 10 to 20.
Array Length
We can get the length of an array using the length property:
int length = numbers.length; // Getting the length of the array
This retrieves the number of elements in the numbers array, which is 5 in this case.
The above are the basic details of Arrays in Java. Now, if you want to go through the in-depth
concepts of Java Arrays, then scroll down below and go through the diagrams, examples, and
explanations.
In-depth Concepts of Java Array
Following are some important points about Java arrays.
Arrays in Java
In Java, all arrays are dynamically allocated.
Arrays may be stored in contiguous memory [consecutive memory locations].
Since arrays are objects in Java, we can find their length using the object
property length. This is different from C/C++, where we find length using size of.
A Java array variable can also be declared like other variables with [] after the data type.
The variables in the array are ordered, and each has an index beginning with 0.
Java array can also be used as a static field, a local variable, or a method parameter.
An array can contain primitives (int, char, etc.) and object (or non-primitive) references of a
class, depending on the definition of the array. In the case of primitive data types, the actual
values might be stored in contiguous memory locations (JVM does not guarantee this behavior).
,In the case of class objects, the actual objects are stored in a heap segment. To learn more
about Java Array, go through Java programming course here.
Note: This storage of arrays helps us randomly access the elements of an array [Support
Random Access].
Creating, Initializing, and Accessing an Arrays in Java
The general form of array declaration is
-- type var-name[];
-- type[] var-name;
An array declaration has two components: the type and the name.
type declares the element type of the array.
The element type determines the data type of each element that comprises the array.
Like an array of integers, we can also create an array of other primitive data types like
char, float, double, etc., or user-defined data types (objects of a class).
Thus, the element type for the array determines what type of data the array will hold.
Example:
// both are valid declarations
int intArray[];
int[] intArray;
// similar to int we can declare
// byte , short, boolean, long, float
// double, char
// an array of references to objects of
// the class MyClass (a class created by user)
MyClass myClassArray[];
// array of Object
Object[] ao,
// array of Collection
// of unknown type
Collection[] ca;
, Although the first declaration establishes that int Array is an array variable, no actual
array exists.
It merely tells the compiler that this variable (int Array) will hold an array of the integer
type.
To link int Array with an actual, physical array of integers, you must allocate one
using new and assign it to int Array.
Instantiating an Array in Java
When an array is declared, only a reference of an array is created. To create or give memory to
the array, you create an array like this: The general form of new as it applies to one-dimensional
arrays appears as follows:
var-name = new type [size];
Here, type specifies the type of data being allocated, size determines the number of elements in
the array, and var-name is the name of the array variable that is linked to the array. To
use new to allocate an array, you must specify the type and number of elements to allocate.
Example:
//declaring array
int intArray[];
// allocating memory to array
intArray = new int[20];
// combining both statements in one
int[] intArray = new int[20];
Note: The elements in the array allocated by new will automatically be initialized to zero (for
numeric types), false (for boolean), or null (for reference types). Do refer to default array values
in Java.
Obtaining an array is a two-step process. First, you must declare a variable of the desired array
type. Second, you must allocate the memory to hold the array, using new, and assign it to the
array variable. Thus, in Java, all arrays are dynamically allocated.
Array Literal in Java
In a situation where the size of the array and variables of the array are already known, array
literals can be used.
// Declaring array literal
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 };
The length of this array determines the length of the created array.
There is no need to write the new int[] part in the latest versions of Java.
Accessing Java Array Elements using for Loop
Each element in the array is accessed via its index. The index begins with 0 and ends at (total
array size)-1. All the elements of array can be accessed using Java for Loop.