Java Arrays - The Coding Shala

Home >> Learn Java >> Java Arrays

Arrays

An array is a collection of the same data type variables. We can access and store the variables in an array by its index. In Java, arrays may have one or more dimensions like a 1-D array, 2-D arrays or multi-Dimensional arrays.
Java Arrays - The Coding Shala

Java One-Dimensional Arrays(1-D Array)


The basic syntax to declare Java 1-D array is as follows - 

type array_name[];

or

type[] array_name;

Here, type declares the data type of the array means which type of data is going to store inside the array. For example - 

int arr[]; 

Here, an array named arr is created having type int. We can store integer values inside arr.

Here, array_name is set to null which means an array with no value. A new operator is used to allocate memory to the array so the basic syntax is as follows - 

type array_name[]  = new type[size];

or

type[] array_name = new type[size];

Here, the size specified the numbers of elements in the array. We can access a specified element in the array by specifying its index within the square bracket.

In Java, all array indexes start at zero. Arrays can be initialized when they are declared. To initialize the array we have to assign a list of comma-separated data or expressions surrounded by curly braces. There is no need to use new in this case.

The following Java program demonstrates the 1-D array - 
class ArrExample {
    public static void main(String[] args){
        int arr[] = new int[5]; //int array arr with size 5
        arr[0] = 1;
        arr[1] = 2;
        arr[2] = 3;
        arr[3] = 4;
        arr[4] = 5;
        System.out.println("Array arr contains at index 1 is: "+ arr[1]);
        
        int arr2[] = { 1, 2, 3, 4, 5}; //same as arr
        System.out.println("Array arr2 contains at index 1 is: " + arr2[1]);
    }
}




Output>>>>>>>>>>>>>>>
Array arr contains at index 1 is: 2
Array arr2 contains at index 1 is: 2

Java Multidimensional Arrays

In Java, multidimensional arrays are arrays of arrays. The following syntax is used to declare a 2-D array - 

type array_name[] [] = new type[size] [size];

Similar we can also declare 3-D array - 

type array_name[] [] [] = new type[size] [size] [size];

The following example explains Java 2-D array - 

class TwoDArray{
    public static void main(String[] args){
        int twoD[][] = new int[4][5]; //ceate 2-D array of 4*5
        int k=1;
        
        for(int i=0;i<4;i++){
            for(int j=0;j<5;j++){
                twoD[i][j] = k;
                k++;
            }
        }
        
        for(int i=0;i<4;i++){
            for(int j=0;j<5;j++){
                System.out.print(twoD[i][j] + " ");
            }
            System.out.println();
        }
    }
}




Output>>>>>>>>>>>>>>>
1 2 3 4 5 
6 7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 

When we allocate memory for the multi-dimensional array, we need to specify the memory for the first(leftmost) dimension. For the remaining dimensions, we can allocate separately. For example - 

int twoD[] [] = new int[4] [];
twoD[0] = new int[5];
twoD[1] = new int[5];
twoD[2] = new int[5];
twoD[3] = new int[5];



Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Java Program to Convert Binary to Decimal - The Coding Shala

N-th Tribonacci Number Solution - The Coding Shala

Shell Script to find sum, product and average of given numbers - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Java Program to Convert Decimal to Binary - The Coding Shala