yt

Header Ads

Chapter -13|| 1-D Arrays with Example in English

Learn  Array in English with Example 

Arrays in Java: 

  • It is a collection of data but same data types.
  • It is used to store group  of data simultaneously.
  • It can store data of same data types means an integer can store only integer arrays can store only integer value, character array can store only character value and so on.
  • We can not fetch data from array directly therefore we use index point. 
  • The indexing  point of array always start with 0
  • Index value is always an integer number.
  • Arrays may be of any data type like int, char, float, etc.

1-D Arrays 

1D Array: It means that, A One-Dimensional Array is the simplest form of an Array in which the elements are stored linearly and can be accessed individually by specifying the index value of each element stored in the array it is called 1D Arrays.

Syntax: 



Syntax Here:- 
  • a is the name of array.
  • int is the data type of array.
  • Size of array is 5 means we can store maximum 5 values in this array.
Initialization of arrays Step-1 : 
int arr[]={45 , 23 , 33 ,89 ,97 ,49};


 

Initialization of arrays Step-2 : 
int arr[]= new int[5];

  • a[0]=5;
  • a[1]=45;
  • a[2]=34;
  • a[3]=36;
  • a[4]=98;
  • a[5]=64;



1- D Arrays Example :- 

 Example 1:  Used step 1 print array elements. 


class Arr1D{
    public static void main(String arg[]){
        int arr[]= {45, 64, 37,30,60};
        System.out.println("Value of arr[0]="+arr[0]);
        System.out.println("Value of arr[1]="+arr[1]);
        System.out.println("Value of arr[2]="+arr[2]);
        System.out.println("Value of arr[3]="+arr[3]);
        System.out.println("Value of arr[4]="+arr[4]);
    }
}


!!!!!!!   Output  !!!!!


javac blog.java // java file
java Arr1D              // java class file
 
Value of arr[0]=45
Value of arr[1]=64
Value of arr[2]=37
Value of arr[3]=30
Value of arr[4]=60



Example 2:  Used step 1 print all array elements using loop.


class Arr1D1{
    public static void main(String arg[]){
        int arr[]= {45, 64, 37,30,60};
        for(int i=0; i<=4; i++)
        System.out.println("Value of arr["+i+"]="+arr[i]);
       
    }
}




!!!!!!!   Output  !!!!!


javac blog.java // java file
java Arr1D1              // java class file
   
Value of arr[0]=45
Value of arr[1]=64
Value of arr[2]=37
Value of arr[3]=30
Value of arr[4]=60



Example 3:  Get user input using loop print all elements.


import java.util.Scanner;
class Arr1D2{
    public static void main(String arg[]){
        Scanner sc= new Scanner(System.in);
        int ar[]=new int[5];
        for(int i=0; i<=4; i++){
            System.out.print("Enter elements arr["+i+"]: ");
            ar[i]=sc.nextInt();
        }
        for(int i=0; i<=4; i++){
            System.out.println("Value of ar["+i+"]="+ar[i]);
        }
    }
}

!!!!!!!   Output  !!!!!


PS F:\Learning File\BCA\BCA 3rd Sem\JAVA OOP\BLOG> javac blog.java
PS F:\Learning File\BCA\BCA 3rd Sem\JAVA OOP\BLOG> java Arr1D2    
Enter elements arr[0]: 4
Enter elements arr[1]: 5
Enter elements arr[2]: 64
Enter elements arr[3]: 12
Enter elements arr[4]: 32

Value of ar[0]=4
Value of ar[1]=5
Value of ar[2]=64
Value of ar[3]=12
Value of ar[4]=32




Guys  Hope 1-D arrays topics are helpful for you. 


No comments

Theme images by Dizzo. Powered by Blogger.