Java

Java Array Explained

An array is a group of identically typed items. For instance, a String array only includes String items and an int array only contains integer elements. The memory’s contiguous places are used to hold the items of an array.

A picture of an array is as follows:

int number[] = new int[10]

Here, the array’s name is number. The array may contain integer values since its type is integer. The array has a 10 element size.

A mechanism based on indexes is used by array. The first member of the array in the above array is represented by number [0], followed by number [1], and so on. The array’s index ranges from 0 to array size-1. In the aforementioned example, the first element’s index is zero and the tenth element’s index is nine.

Advantages of Array

Better performance: Since an array uses an index-based approach to search its elements, it performs a variety of actions more quickly.

Multidimensional: Arrays, as opposed to ArrayList, which is one dimensional, are multidimensional. Examples are 2D arrays and 3D arrays.

Faster access: An array makes it simple to access an element.

Disadvantages of Array:

Fixed Size: The array’s size is fixed and cannot be expanded in the future.

Allows only elements of the same type: Because arrays are homogenous, they cannot include elements of various types. For instance, an integer array cannot contain strings, and a string array cannot contain components of the same type as an integer array.

Shifting of pieces is necessary for insertion and delegation.

Declaration, Instantiation and Initialization of Array in Java

This is how an array is declared, created, and initialized.

int number[]; //array declaration
number[] = new int[10]; //array instantiation
number[0] = 10; //array Initialization
number[1] = 20; //array Initialization

Another way to declare an array is as follows: The three following syntaxes are all acceptable for declaring an array.

int[] number; 
int []number;  
int number[];

The example that follows shows how to define an int array, populate it with integers, then use a for loop to display the array’s members.
As you can see, we utilized the array’s length attribute to get the array’s size. The length property of an array returns the array’s element count.

public class JavaExample{
  public static void main(String args[]){
    //array declaration, instantiation and initialization
    int number[] = {11, 22, 33, 44, 55};

    //print array elements
    //length property return the size of the array
    for(int i=0;i<number.length;i++)
      System.out.println("number["+i+"]: "+number[i]);
  }
}

Output:

Java Array Explained

Types of array in Java

  1. Single Dimensional Array
  2. Multidimensional Array

1. Single dimensional array

public class JavaExample{
  public static void main(String args[]){
    //array declaration
    String names[] = new String[5];

    //array initialization
    names[0]="Chaitanya";
    names[1]="Ajeet";
    names[2]="Rahul";
    names[3]="Shivam";
    names[4]="Rohit";

    //print array elements
    for(int i=0;i<names.length;i++)
      System.out.println("names["+i+"]: "+names[i]);
  }
}

Output:

names[0]: Chaitanya
names[1]: Ajeet
names[2]: Rahul
names[3]: Shivam
names[4]: Rohit

2. Multidimensional array

Multidimensional array declaration:

Here is how to define a multidimensional array: The declaration of a multidimensional array is valid for all four syntaxes.

int[][] arr; 
int [][]arr;
int arr[][];
int []arr[];

Instantiate Multidimensional Array in Java
The multidimensional array’s element count is equal to the product of its row and column counts.
The following array has a maximum capacity of 2 * 3 = 6.

int[][] arr=new int[2][3]; //2 rows and 3 columns

Initialize Multidimensional Array in Java

arr[0][0]=11;  
arr[0][1]=22;  
arr[0][2]=33;  
arr[1][0]=44;  
arr[1][1]=55;  
arr[1][2]=66;

Example:

public class JavaExample{
  public static void main(String args[]){
    //two rows and three columns
    int arr[][]={{11,22,33},{44,55,66}};

    //outer loop 0 till number of rows
    for(int i=0;i<2;i++){
      //inner loop from 0 till number of columns
      for(int j=0;j<3;j++){
        System.out.print(arr[i][j]+" ");
      }
      //new line after each row
      System.out.println();
    }
  }
}

Output:

Java Array Explained

Print an Array elements using for-each loop

We have already learned how to use a for loop to print an array’s elements. Without utilizing the array length property, there is another way to print an array’s items.

public class JavaExample{
  public static void main(String args[]){
    //String array
    String names[]={"Chaitanya", "Ajeet", "Rahul", "Hari"};

    //print array elements using for-each loop
    for(String str:names)
      System.out.println(str);

    //int array
    int numbers[]={1, 2, 3, 4, 5};

    //print array elements using for-each loop
    for(int num:numbers)
      System.out.println(num);
  }
}

Output:

Chaitanya
Ajeet
Rahul
Hari
1
2
3
4
5

Exception: ArrayIndexOutOfBoundsException

ArrayIndexOutOfBoundsException occurs when we use an invalid index to retrieve an array. When the index is either negative, larger than, or equal to the array size, this occurs.

public class JavaExample{
  public static void main(String args[]){
    int number[]={1, 5, 7, 9, 11};
    for(int i=0;i<=number.length;i++){
      System.out.println(number[i]);
    }
  }
}

Output:

Java Array Explained

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button