Array in Java


An array is a collection of elements of the same type placed in contiguous memory locations that can be individually referenced by using an index to a unique identifier.

  • Array Type : Type of the array. This can be primitive (int, long, byte) or Objects (String, MyObject, etc).
  • Index : Index refers to the position of a certain Object in an array.
  • Length : Every array, when being created, needs a set length specified. This is either done when creating an empty array (new int[3]) or implied when specifying values ({1, 2, 3}).

  Syntax:
     Datatype variable_name [ ] ;
     (or)
     Datatype [ ] variable_name ;

  Example :
    Int [ ] array = new int [ 5 ] ;

This program demonstrates the use of arrays in Java. It defines an array a of ten integers with values 10, 20, 30, ..., 100.

  • The program then accesses and prints the third element of the array using System.out.println(a[2]);.
  • Next, the program uses a for loop to print all the elements of the array a. The loop iterates from 0 to the length of the array minus 1, and uses System.out.println(a[i]); to print each element of the array.
  • The program then uses an enhanced for loop to print all the elements of the array a. The enhanced for loop iterates over each element in the array and assigns it to the variable element, which is then printed using System.out.println(element);.
  • The program then declares and initializes two arrays b and c with ten integer elements each. It demonstrates that by default, all elements in the array have a value of zero.
  • The program then prompts the user to enter three integer values, which are stored in the first three elements of array c using a for loop and the Scanner class. The program then uses an enhanced for loop to print all the elements of the array c.

Source Code

import java.util.Scanner;
public class One_Array {
    //Arrays in Java
    public static void main(String args[])
    {
        int a[]={10,20,30,40,50,60,70,80,90,100};
        //Accessing Elements in array
        System.out.println(a[2]);
 
        //Print all Elements using for loop
        for(int i=0;i<a.length;i++)
        {
            System.out.println(a[i]);
        }
    //Print all Elements using Enhanced for loop
       for(int element : a)
       {
           System.out.println(element);
       }
 
        int b[]; // Declaring array
        b=new int[10]; // Allocating Memory to Array
        int [] c =new int[10]; //Combining Both Statement
 
        //Buy default all element have zero value
        for(int element : b)
        {
            System.out.println(element);
        }
 
        for(int i=0;i<3;i++)
        {
            Scanner in =new Scanner(System.in);
            System.out.println("Enter The Number");
            c[i]=in.nextInt();
        }
        for(int element : c)
        {
            System.out.println(element);
        }
 
    }
}
 

Output

//Accessing Elements in array
30  

//Print all Elements using for loop
10
20
30
40
50
60
70
80
90
100

//Print all Elements using Enhanced for loop
10
20
30
40
50
60
70
80
90
100

//Buy default all element have zero value
0
0
0
0
0
0
0
0
0
0

//User input array values
Enter The Number
1
Enter The Number
2
Enter The Number
3
1
2
3
0
0
0
0
0
0
0
To download raw file Click Here

Basic Programs