Arrays in Java Programming || One Dimensional Array in Java || Java

What are 'Arrays' ?

An Array is known as the collection of Similar data types.
An Array is actually a data structure which consists of a group of elements of same type.

Arrays store same type of elements in consecutive memory location one after another.

Examples :- 
Suppose there is an array which consists of 5 Elements of integer type let's say the elements are 4,6,2,7,9.

So, if in the above array the address of the memory location of the element 4 is 'a123480' then the address of the memory location of the next element 6 will be 'a123481' and that of the next element i.e. 2 will be 'a123482' and further elements will be followed.

array element                address
4                                        'a123480'
6                                        'a123481'
2                                        'a123482'
7                                        'a123483'
9                                        'a123484'


Now let's dig into how to declare and create an array in Java programming language: 

Syntax:
Arrays

Now Lets see an example of Array in Java:

 package com.company;
 import java.util.*;

 public class Main 
 {

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
	    System.out.println("Enter the Size of Array");
	    int n = sc.nextInt();
		
        // declaring array 
	    int[] arr = new int[n];
        
        // for loop to take the array elements from the user
        System.out.println("Enter the elements of Array");
	    for (int i=0; i<n;i++)
        {
            arr[i] = sc.nextInt();
        }
		
        // printing the array elements
	    System.out.println("The Inputed values are :-");
	    for (int i=0;i<n;i++)
        {
            System.out.println(arr[i]);
        }

    }
 }

Output For Java


#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post