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.
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.
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); } } }
//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 0To download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions