Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

Array

Normally an array is a collection of similar types of elements and has contiguous memory location.

Following are the salient features of Java array:

• Array uses index representation to store elements which always start from 0.
• Array follows added order to store the elements. (i.e., when we store first element it is stored at 0th index by default, then next element is stored at 1st index and so on).
• Random access in allowed which means using the index position we can get the value directly.¬¬
• An array stores homogeneous type of data.
• In Java, an array is an object and is allocated memory inside the heap.
• In Java, array works on Dynamic Memory Allocation, i.e., the JVM calculates the size of an array at runtime.
• Length of an array represents the number of elements that have been stored inside it
• Maximum length of an array in Java is 2^31-1 which is equivalent of 2147483647.

Types of Arrays:

1. Static Array: - In the case of static array, we initialize it at the time of its declaration

Syntax: -

<data_type> <array_name> [ ] = {value1, value2, value3…};

Example: - int ar1 [ ] = {10, 20, 30, 40, 50};

In the above example we have an int type array and its length is 5. The numbers stored in the array have values 10, 20, 30, 40 and 50; ar1 is a reference variable which stores the reference of the corresponding array.

2. Dynamic Array: - In dynamic array first we create an array, then as per the requirement we take the values from different sources like keyboard, files and database dynamically.

Syntax: -

<data_type> <array_name> [ ] = new <data_type>[length_of_array];

Example: - int ar1[ ] = new int [5];

In the above example we have an int type array and its length is 5 and the values are default values of int data type. ar1 is a reference variable which has the reference of the corresponding array.

Types of Arrays:

3. Single-Dimensional Array.
4. Multi-Dimensional Array.

3. Single-Dimensional Array

A single dimensional array in Java contains elements directly, that is the reason a Single-Dimensional Array is also called Array of Elements.

            
public class JTC {

public static void main(String[ ] args) {

    int[ ] ar1 = { 5, 6, 3, 2, 1 };

    // Print all the array elements
    for (int i = 0; i < ar1.length; i++) {
        System.out.println("Index :- "+i+" Value :- "+ar1[i]);
    }
}

}

            
        
    Index :- 0 Value :- 5
    Index :- 1 Value :- 6
    Index :- 2 Value :- 3
    Index :- 3 Value :- 2
    Index :- 4 Value :- 1
            

In the above program we have an int type of one dimentional or single dimentional array which is ar1 and then using for-loop we are trying to print values from 0th Index to last index.


4. Multi-Dimensional Array

In Java, a multi-dimensional array is an array which contains the reference of other arrays, that is the reason it is also known as Array of Arrays.

Types of Multi-Dimensional Arrays

1. Two-Dimensional Array
2. Three-Dimensional Array

Two-Dimensional Array

• Two-dimensional Array is also called 2D Array.

Syntax: -

<data_type> <array_name> [ ][ ] = {{value1, value2, value3…},{value1, value2, value3…},{value1, value2, value3..},{…},{…},….};

Example: - int ar1[ ][ ] = {{101,202,303},{404,505,606},{707,808,909}};

In the above example we have an int type of 2-D static array. Here we will get four different arrays. The first array is of int [ ] type and its reference is stored in the ar1 reference variable. Then we have three more arrays of int data type and their references are stored inside the 0th, 1st and 2nd indexes respectively.

            
class JTC {

public static void main(String[ ] args) {
    int ar1[ ][ ] = {{101,202,303},{404,505,606},{707,808,909}};
    
    for (int i = 0; i < ar1.length; i++){
        for (int j = 0; j < ar1[i].length; j++){
            System.out.print("ar1["+i+"]["+j+"] = "+ar1[i][j] + " ");
}
System.out.println();
}

}
}

            
        
    ar1[0][0] = 101 ar1[0][1] = 202 ar1[0][2] = 303
    ar1[1][0] = 404 ar1[1][1] = 505 ar1[1][2] = 606
    ar1[2][0] = 707 ar1[2][1] = 808 ar1[2][2] = 909
            


In the above program we have int type 2-D static array and using nested for-loop we are traversing that array along with their index positions.

syntax:

<data_type> <array_name> [ ][ ] = new <data_type>[length1][length2];

Example: - int ar1[ ][ ] = new int[3][3];

In the above example we have an int type of 2-D dynamic array. Here we will get four different arrays. The first array is of int [] type and its reference is stored in the ar1 reference variable. Then we have three more arrays and their values are default values of int which is 0 and their data type will be int and their references are stored inside the 0th , 1st and 2nd indexes respectively.

            
class JTC {

public static void main(String[] args) {
    int ar1[][] = new int[3][3];
    
    for (int i = 0; i < ar1.length; i++){
        for (int j = 0; j < ar1[i].length; j++){
            System.out.print("ar1["+i+"]["+j+"] = "+ar1[i][j] + " ");
}
System.out.println();
}

}
}
            
        
    ar1[0][0] = 0 ar1[0][1] = 0 ar1[0][2] = 0
    ar1[1][0] = 0 ar1[1][1] = 0 ar1[1][2] = 0
    ar1[2][0] = 0 ar1[2][1] = 0 ar1[2][2] = 0
            


In the above program we have int type 2-D dynamic array and using nested for-loop we are traversing that array along with their index positions.

4. Three-Dimensional Array

• Three-Dimensional Array is also called 3D array.

syntax:

static 3-D Array: -

<data_type> <array_name> [ ][ ][ ] = {{{v1,v2,v3},{v1,v2,v3},{v1,v2,v3}},{{v1,v2},{v1}}};

Dynamic 3-D Array: -

data_type [ ] [ ] [ ] array_name = new data_type [x1][x2][x3];

Example: - int ar1[ ][ ][ ] = {{{1,2,3},{4,5,6}},{{6,7,8},{56,3,-4},{12,0,43}}};

In the above example first we have an array with length as 2 containing 2 array references at its 0th and 1st index respectively and further arrays are also containing the references of other arrays.

            
class JTC{

public static void main(String[] args) {

int ar1[][][] = {{{1,2,3},{4,5,6}},{{6,7,8},{56,3,-4},{12,0,43}}};

for(int i = 0; i < ar1.length; i++){
    for(int j = 0; j < ar1[i].length; j++){
        for(int k = 0; k < ar1[i][j].length; k++){
            System.out.print(ar1[i][j][k] + " ");
}
System.out.println();
            }
        } 
    }
}
    1 2 3
    4 5 6
    6 7 8
    56 3 -4
    12 0 43
            


In the above program we have int type 3-D static array and using nested for-loop we are traversing that array along with their index positions.

Example: -int ar1[ ][ ][ ] = new int[3][3][3];

In the above example we have 3-D dynamic array, in the diagram we can see that the first array contains the references of further 3 arrays and these further 3 arrays also contain the references of other arrays.

            
class JTC{

public static void main(String[] args) {

int ar1[][][] = new int[3][3][3];

for(int i = 0; i < ar1.length; i++){
    for(int j = 0; j < ar1[i].length; j++){
        for(int k = 0; k < ar1[i][j].length; k++){
            System.out.print(ar1[i][j][k] + " ");
}
System.out.println();
            }
        }
    }
}
    0 0 0
    0 0 0
    0 0 0
    0 0 0
    0 0 0
    0 0 0
    0 0 0
    0 0 0
    0 0 0
            

In the above program we have int type 3-D dynamic array and using nested for-loop we are traversing that array along with their index positions.

Command Line Argument:

During execution, we can only use a .class file that contains the main method; otherwise, a MainMethodNotFoundError occurs. Therefore, in order to run a .class file, it is essential to declare the main method.

Main method declaration:

public static void main(String arg[]){
}

Access Modifier: public
Method Type: Static Method
Return Type: void
Method Name: main
Method Parameter: String arg[]

In this tutorial, we will explore the purpose of the main method parameter, which is a String array (String[]).

The String[] arg contains the input provided at the time of executing the program from the command line, which is also known as Command Line Argument.

            
class JTC{
	public static void main(String arg[]){
		for(int i = 0; i <= arg.length-1; i++){
			System.out.println("arg["+i+"] :- "+arg[i]);
		}
	}
}
    C:\Users\JTC\OneDrive\Desktop>java JTC Hello Hai true 1 12.34 A
    arg[0] :- Hello
    arg[1] :- Hai
    arg[2] :- true
    arg[3] :- 1
    arg[4] :- 12.34
    arg[5] :- A
            

In the above example, as we can see in the main method, there is a for loop that iterates through the String[] arg array and prints its values.
An important point to note in the console is that during execution, we pass different arguments, which are stored in the String[] arg array in the order they are added. We can use their index positions to access these command line arguments. It's essential to remember that all command line arguments are in String format.
To convert String Command Line Arguments, we can use the parseXXX() method, which parses the data from String to the corresponding type. These methods are available in the corresponding wrapper classes, such as parseByte(), parseShort(), parseInt(), parseLong(), parseFloat(), parseDouble(), parseBoolean() in the java.lang.Byte, java.lang.Short, java.lang.Integer, java.lang.Long, java.lang.Float, java.lang.Double, and java.lang.Boolean classes, respectively.

Ques:- Why main method parameter is String type array?

Ans:- The JVM allows us to declare a main method with a String type array; otherwise, we encounter a MainMethodNotFoundError. This is because String is the only literal that allows us to define data in different formats, such as: 'true', '10', 'A', '12.34', and so on.