print
Advertisment
Advertisment

Arrays

Array is a group of similar data types Array is a container object used to store variables of similar data types. ... for example, if the array size is declared as 10, it can store array elements from 0 to 9 index.

Example

		
		public class Student {
			public static void main(String agrs[]){
				String name = "Ramakant rawat";
				int arr[] = new int[] {5,6,7,8,9}
				System.out.println(arr[3])//8 print because 8 is located at third memory location
			}
		}
		
	

Output :

	
		8
	

Types of Array

  1. Single Dimensional Array
  2. Multi Dimensional Array

Single Dimensional Array

Array containing only one subscript called a single dimensional array

Syntax

Data type arr[];

Data type []arr;

Example

Let's see the simple example of single dimensional array, where we are going to discurse declare, initialize, instantiate and traverse an array.

		
		class Test{
			public static void main(String agrs[]){
				int arr[] = new int[5];//difine array declaretion and instantiate
				arr[0] = 5  ; //array initialization
				arr[1] = 6;
				arr[3] = 10;
				arr[4] = 20;
				arr[5] = 25;
				System.out.println(arr[0]);
				System.out.println(arr[1]);
			}
		}
			
	

Output :

	
		6
		10

	

Multi Dimensional Array

An array containing only two or more subscripts called a multidimensional array

Syntax

Data type arr[][[][]...[];

Data type [][]arr;

Example

Let's see the simple example of single dimensional array, where we are going to discurse declare, initialize, instantiate and traverse an array.

		
		class Test{
			public static void main(String agrs[]){
				int arr[][] = new int[5][2];//difine array declaretion and instantiate
				arr[0][0] = 5  ; //array initialization
				arr[0][1] = 10;
				arr[1][0] = 20;
				System.out.println(arr[0][0]);
				System.out.println(arr[0][1]);
				System.out.println(arr[1][0]);
			}
		}
			
	

Output :

	
		5
		10
		20
	

Advantage and disadvantage Array in java

Advantage

  • Array can be store multiple value in a single variable.
  • Array can be easily implemented.
  • less time complaxcity.
  • We can store objects in an array.

Advantage

  • We can not increase and dcrease size of array at runtime.
  • It can be store similar value.
  • Memory is wasted by Array.
Advertisment

        

Advertisment
Advertisment
arrow_upward