Table of Contents
In this article, you will learn about arrays in java, Features of Arrays, Single Dimensional Arrays, Foreach loop and Multidimensional Arrays with examples.
An array is a container object that contains the similar type of data. It can hold both primitive and object type data.
Each item in an array is called an element and each element is accessed by its numeric index.
Features of Arrays
- Retrieve and sort the data easily.
- Arrays are created during runtime.
- Arrays can hold the reference variables of other objects.
- Arrays are fixed in size.
How to declare an array in Java?
In Java, here is how we can declare an array.
dataType[] arrayName;
- dataType – it can be primitive data types like
int
,char
,double
,byte
, etc. or Java objects. - arrayName – it is an identifier.
For example,
double[] data;
Here, data is an array that can hold values of type double
.
But, how many elements can array this hold?
Good question! To define the number of elements that an array can hold, we have to allocate memory for the array in Java. For example,
// declare an array
double[] data;
// allocate memory
data = new Double[10];
Here, the array can store 10 elements. We can also say that the size or length of the array is 10.
In Java, we can declare and allocate memory of an array in one single statement. For example,
double[] data = new double[10];
How to Initialize Arrays in Java?
In Java, we can initialize arrays during declaration. For example,
//declare and initialize and array
int[] age = {12, 4, 5, 2, 5};
Here, we have created an array named age and initialized it with the values inside the curly brackets.
Note that we have not provided the size of the array. In this case, the Java compiler automatically specifies the size by counting the number of elements in the array (i.e. 5).
In the Java array, each memory location is associated with a number. The number is known as an array index. We can also initialize arrays in Java, using the index number. For example,
// declare an array
int[] age = new int[5];
// initialize array
age[0] = 12;
age[1] = 4;
age[2] = 5;
Note:
- Array indices always start from 0. That is, the first element of an array is at index 0.
- If the size of an array is n, then the last element of the array will be at index n-1.
How to Access Elements of an Array in Java?
We can access the element of an array using the index number. Here is the syntax for accessing elements of an array,
// access array elements
array[index]
Types of Arrays
1. Single dimensional arrays
2. Multidimensional arrays
Single Dimensional Arrays
Declaration of Single Dimensional Arrays
Give the name and type to the arrays called declaration of the array.
For example,
- int[ ] arr or int [ ]arr or int arr[ ];
- byte[ ] arr;
- short[ ] arr;
- long[ ] arr;
- char[ ] chr;
- String[ ] str ect.
Creating, Initializing and Accessing an Array
int arr = new int[10]; // creation of integer array of size 10
Example: An example of single dimensional array in Java.
class ArrayDemo
{
public static void main(String args[])
{
int arr[] = {2, 4, 5, 7, 9, 10};
for(int i = 0; i < arr.length; i++)
{
System.out.println("arr["+i+"] = "+arr[i]);
}
}
}
Output
arr[0] = 2 arr[1] = 4 arr[2] = 5 arr[3] = 7 arr[4] = 9 arr[5] = 10
Example: A java program to find the maximum and minimum number in the arrays.
class MaxMinDemo
{
static void max(int arr[])
{
int max = arr[0];
for( int i = 1; i < arr.length; ++i)
{
if(arr[i] > max)
max = arr[i];
}
System.out.println("Max value is: "+max);
}
static void min(int arr[])
{
int min = arr[0];
for( int i = 1; i < arr.length; ++i)
{
if(arr[i] < min)
min = arr[i];
}
System.out.println("Min value is: "+min);
}
public static void main(String args[])
{
int arr[] = {10, 2, 7 , 3, 16, 21, 9};
max(arr);
min(arr);
}
}
Output
Max value is: 21 Min value is: 2
The foreach loop
The foreach loop introduced in JDK 1.5 provides the traverse the complete array sequentially without using index variable.
Example: A simple program to understand the foreach loop.
class ForEachDemo
{
public static void main(String[] args)
{
int[] arr = {3, 5, 7, 11, 13, 17};
for (int element: arr)
{
System.out.print(element+" ");
}
}
}
Output
3 5 7 11 13 17
Multidimensional Arrays
In multidimensional array data is stored in form of rows and columns.
Declaration of Multidimensional Arrays
- int arr[ ][ ], or int[ ][ ] arr, or int [ ][ ]arr.
- char arr[ ][ ] or char[ ][ ] arr or char [ ][ ]arr
- double arr[ ][ ] or double[ ][ ] arr or double [ ][ ]arr.
Example: A sample program to create a multidimensional array.
class MultiDimeDemo
{
public static void main(String args[])
{
int arr[] [] = {{9,8,7},{6,5,4},{3,2,1}};
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
Output
9 8 7 6 5 4 3 2 1
Copying Arrays
System class has a method called arraycopy(), that is used to copy data from one array to another.
Example: Simple program to copy data from one array to another.
class Demo
{
public static void main(String[] args)
{
char[] source = { 'a', 'T', 'e', 'c', 'h', 'a', 'r','g', 'e' };
char[] dest = new char[8];
System.arraycopy(source, 1, dest, 0, 8 );
System.out.println(new String(dest));
}
}
Output:
Techarge
Thank you for reading, If you have reached so far, please like the article, It will encourage me to write more such articles. Do share your valuable suggestions, I appreciate your honest feedback!