Java Arrays
Table of Contents
Java Arrays
Java Arrays
An array in Java is a grouping of related data types. An object called an array serves as a container for values of the same type. Because an array’s size must be declared at the time of declaration, it is also referred to as a static data structure.
Array starts from zero index and goes to n-1 where n is length of the array.
So In Java, array is treated as an object and stores into heap memory. It allows to store pri-mitive values or reference values.
Array can be single dimensional or multidimensional in Java.
Features of Java Arrays
- It is always indexed. Index begins from 0.
- It is a collection of similar data types.
- It occupies a contiguous memory location.
- It allows to access elements randomly.
Single Dimensional Array
A single index is used to store elements in a single-dimensional array. Simply increasing an array’s index by one will return all of its elements.
Array Declaration
Syntax :
datatype[] arrayName;;
or
datatype arrayName[];
Java allows to declare array by using both declaration syntax, both are valid.
So The arrayName can be any valid array name and datatype can be any like: int, float, byte etc.
Example :
int[ ] arr;
char[ ] arr;
short[ ] arr;
long[ ] arr;
int[ ][ ] arr; // two dimensional array.
Initialization of Array
Memory allocation for an array takes place during initialization. We specify the array’s size at initialization in order to set aside memory space.
Initialization Syntax
arrayName = new datatype[size]
new
operator is used to initialize an array.
So The arrayName is the name of array, new is a keyword used to allocate memory and size is length of array.
We can combine both declaration and initialization in a single statement.
Datatype[] arrayName = new datatype[size]
Example : Create An Array
Lets create a single dimensional array.
class Demo
{
public static void main(String[] args)
{
int[] arr = new int[5];
for(int x : arr)
{
System.out.println(x);
}
}
}
0 0 0 0 0
In the example above, we created an array called arr, which can hold 5 elements and is of the int type. The array prints five times zero to the console when we iterate through it to access its elements. Because we didn’t assign values to the array, all of its elements were initially initialised to 0, which is why it prints 0.
Set Array Elements
We can set array elements either at the time of initialization or by assigning direct to its index.
int[] arr = {10,20,30,40,50};
Here, we are assigning values at the time of array creation. It is useful when we want to store static data into the array.
or
arr[1] = 105
Here, we are assigning a value to array’s 1 index. It is useful when we want to store dynamic data into the array.
Java Arrays Example
Here, we are assigning values to array by using both the way discussed above.
class Demo
{
public static void main(String[] args)
{
int[] arr = {10,20,30,40,50};
for(int x : arr)
{
System.out.println(x);
}
// assigning a value
arr[1] = 105;
System.out.println("element at first index: "
+arr[1]);
}
}
10 20 30 40 50 element at first index: 105
Accessing array element
So By using an array element’s index value, we can access it. Using a loop or a direct index value, as appropriate. To iterate through the elements of an array, we can use a loop like for, for-each, or while.
Example to access elements
class Demo
{
public static void main(String[] args)
{
int[] arr = {10,20,30,40,50};
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
System.out.println("element at first index: "
+arr[1]);
}
}
10 20 30 40 50 element at first index: 20
So Here, we are traversing array elements using loop and accessed an element randomly.
Note:-To find the length of an array, we can use length property of array object like: array_name.length
.
Multi-Dimensional Array
A single dimensional array and a multi-dimensional array are very similar. In contrast to a single-dimensional array, which can only have one row index, it can have multiple rows and multiple columns. Data is represented in a tabular format with rows and columns for storage.
Multi-Dimensional Array Declaration
datatype[ ][ ] arrayName;
Initialization of Array
datatype[ ][ ] arrayName = new int[no_of_rows][no_of_
columns];
The arrayName is the name of array, new is a keyword used to allocate memory and no_of_rows and no_of_columns both are used to set size of rows and columns elements.
So Like single dimensional array, we can statically initialize multi-dimensional array as well.
int[ ][ ] arr = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}
};
Java Arrays Example:
class Demo
{
public static void main(String[] args)
{
int arr[ ][ ] = {{1,2,3,4,5},{6,7,8,9,10},{11,12,
13,14,15}};
for(int i=0;i<3;i++)
{
for (int j = 0; j < 5; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
// assigning a value
System.out.println("element at first row and
second column: " +arr[0][1]);
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 element at first row and second column: 2
Jagged Array
Jagged array is an array that has different numbers of columns elements. In java, a jagged array means to have a multi-dimensional array with uneven size of columns in it.
Initialization of Jagged Array
Jagged array initialization is different little different. We have to set columns size for each row independently.
int[ ][ ] arr = new int[3][ ];
arr[0] = new int[3];
arr[1] = new int[4];
arr[2] = new int[5];
Example : Jagged Array
class Demo
{
public static void main(String[] args)
{
int arr[ ][ ] = {{1,2,3},{4,5},{6,7,8,9}};
for(int i=0;i<3;i++)
{
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
1 2 3 4 5 6 7 8 9
So Here, we can see number of rows are 3 and columns are different for each row. So This type of array is called jagged array.