Array in Java with Example | How to Initialize Array in java

Array in Java is an object that holds multiple values in a single variable. The array can hold the value of only a similar data type. If you want to represent the huge number of values by using a single variable then you can go for Array. We can create an array of any type and may have one or more dimensions. The length of an array is established when an array is created. After creation, the length of the array is fixed. Array in Java is based on the index. Each item in an array called an element, and each element is accessed by its index number. The index number begins with 0 and the 9th element, for example, would, therefore, be accessed at index 8.

Array in java with example

Advantages of Array:

1. We can represent a huge number of values by using a single variable.
2. We can easily search any element of an array by using the index number.


Disadvantages of Array:

1. Arrays are fixed in size. The length of an array is established when an array is created. Once we create an array with some size then there is no chance of increasing or decreasing the size of an array based on our requirements.
2. Arrays can hold only homogenous data type of elements.

Note: To overcome the limitation of an array we should go for collection in Java.




Types of Array in java:

There are two types of Array in Java:
1. One-dimensional Array.
2. Multidimensional Array.


How to declare Array in Java?

We can declare an array by using different ways. The general form of a one-dimensional array declaration is:


data-type[] variable-name; Or
data-type []variable-name; Or
data-type variable-name[];
All the above-mentioned declaration is valid. You can use any one of the above declarations. Here data-type describe the what type of data the array will hold and variable-name describe the reference of the array.


How to instantiate an Array in Java?

Once an array is declared, the only reference of an array is created. But you need to use it now to allocate memory for arrays. We can use the new keyword to instantiate an array. The general form of instantiating an array appears as follows:

variable-name = new data-type[size];
Here data-type specifies the type of data being allocated, size specifies the number of elements in the array, and variable-name specifies the reference of an array that is referred to the array. 

How to initialize an array in Java?

Arrays can be initialized when they declared or later in the program as per your requirements. We can initialize the array by a list of comma-separated expressions surrounded by curly braces. The commas separate the value of the array elements. The general form of one-dimensional array initialization is as follows:

int array[] = {10, 20, 40, 50}; OR
arrayRefvarName[index-number] = value;


Example of Array in Java

Let's understand the concept of an array with an example. In this example, we create an array of int type, we initialize the array and traverse the array to print the values of array.

class ArrayExample{
	public static void main(String args[]){
		//Array declaration.
		int[] array;
		
		// Array instantiate.
		array = new int[5];
		
		//Array initialization.
		array[0] = 10;
		array[1] = 20;
		array[2] = 30;
		array[3] = 40;
		array[4] = 50;
		
		//traverse the array.
		for(int i = 0; i<array.length;i++){
			System.out.println("Element at index " +i+ " is: " +array[i]);
		}
	}
}

Output:

Element at index 0 is: 10
Element at index 1 is: 20
Element at index 2 is: 30
Element at index 3 is: 40
Element at index 4 is: 50


Multidimensional Array in Java:

Multidimensional Arrays in Java are actually arrays of arrays. The elements of a multidimensional array are stored in the form of row and column also known as matrix form



How to declare a Multidimensional Array in Java? 

The general form of declaration of Multidimensional Array is as follow:

data-type[][] arrayRefVarName; (OR)
data-type arrayRefVaName[][];  (OR)
data-type [][]arrayRefVarName;


How to instantiate a Multidimensional Array?

Once an array is declared, the only reference of an array is created. But you need to use it now to allocate memory for arrays. The general form of instantiating a Multidimensional array appears as follows:

int[][] array = new int[2][2] //2 row & 2 column


How to initialize a Multidimensional array in Java?

Multidimensional Arrays can be initialized when they declared or later in the program as per your requirements. The general form of multidimensional array initialization is as follows:

int[][] array = {{1,2,3}, {4,5,6}, {7,8,9}};


Example of Multidimensional Array in Java:

Let's see a simple example to understand the Multidimensional array. In this example, we declare and initialize the multidimensional array and we can also print the value of this array in the form to row and column.

class ArrayExample{
	public static void main(String args[]){
		
		//Declaration and initialization.
		// of 2-D array
		int[][] array = {{1,2,3}, {4,5,6}, {7,8,9}};
		
		//loop for row and column.
		for(int i=0; i<3; i++){
			for(int j=0;j<3;j++){
				//print the elements of 2-D array.
				System.out.print(array[i][j] + " ");
			}
			System.out.println();
		}
	}
}

Output:

1 2 3
4 5 6
7 8 9


How to copy data from one array to another?

The System class has an arraycopy method that you can use to efficiently copy data from one array into another. The syntax is given below:

public static void arraycopy(Object src, int srcPos,
                             Object dest, int destPos, int length);
The two Object arguments specify the array to copy from and the array to copy to. The three int arguments specify the starting position in the source array, the starting position in the destination array, and the number of array elements to copy. 



Example of copying an array in java:

In this example, we can declare an array of char elements. It uses the System.arraycopy method to copy a subsequence of array component into the second array. Let's see the example.

class ArrayCopy{
    public static void main(String[] args){
		
		//declaring a source array.
        char[] copyFrom = { 'd', 'e', 'j', 'a', 'v', 'a', 's',
			    't', 'u', 'd', 'y', 'p', 'o','i','n','t'};
				
		 //declaring a destination array.		
        char[] copyTo = new char[14];
		
         //copying an array using arraycopy method.
        System.arraycopy(copyFrom, 2, copyTo, 0, 14);
		
		 //printing the destination array.
        System.out.println(new String(copyTo));
    }
}

Output:

javastudypoint



You may also like these posts:



Array in Java with Example | How to Initialize Array in java Array in Java with Example | How to Initialize Array in java Reviewed by Prashant Srivastava on December 28, 2019 Rating: 5

No comments:

Powered by Blogger.