java arrays - CodingAliens

What is Array?


Array ek common type data structure h ,isme sare elements  same data type ke hote h. Ek bar agar array define kr diya to uska size fixed rehta h aur size ko increase ni kia ja skta aur jada elements ke liye.Array me first element zero index se start hota h.

Bole to,yah ek programming concept hai jo niche digayi problem ko short krne me help krta h.
x0=0;
x1=1;
x2=2;
x3=3;
isake sath...
x[0]=0;
x[1]=1;
x[2]=2;
x[3]=3;
Is tutorial me aap sekhenge -
  • What is an array?
  • Array Variables
  • First Array Program
  • Java Array: Pass by reference
  • Multidimensional arrays

Printing array elements with a for loop in jav

for(i=0; i<5; i++) {
     System.out.print(x[i]);
   }

Array Variables

Using an array in your program is a 3 step process -
1) Declaring your Array
2) Constructing your Array
3) Initialize your Array

1) Declaring your Array

Syntax
<element ka Type>[] <array ka Name>;
or
 <element ka Type> <array ka Name>[];
Example:
int intArray[];   // intArray ek ARRAY variable h jo integer values ko store krta h
int []intArray; or int intArray[]; 

2) Constructing an Array

 arrayname = new dataType[]
Example:
intArray = new int[10]; // Defines that intArray will store 10 integer values
Declaration and Construction combined
int intArray[] = new int[10];

3) Initialize an Array

intArray[0]=0; // Assigns an integer value 0 to the first element 0 of the array

intArray[1]=1; // Assigns an integer value 1 to the second element 1 of the array
Declaring and initialize an Array
[]  = {};
Example:
 int intArray[] = {1, 2, 3, 4};

First Array Program

Step 1) Same code copy kre apne editor me.
class Array{
     public static void main(String arg[]){
        int array[] = new int[5];
        for (int i=0;i<5;i++){
           array[i]=i+1;
       }
       for (int j=0;j<5;j++){
           System.out.println("array["+j+"] = "+array[j]);
       }
      }
}
Step 2) Save , Compile aur Run the code.
Output:
array[0] = 1
array[1] = 2
array[2] = 3
array[3] = 4
array[4] = 5
Step 3) Agar x ek array ka naam h ya usko represent kr rha h ,then  x.length  aapko array ki length dega

Save, Compile aur Run the code.
System.out.print("Length of Array  =  "+array.length)
//output
Length of Array  =  5

Java Array: Pass by reference

Arrays are passed to functions by reference, or as a pointer to the original.Iska matlb yah h ki  aap  function me array ke liye kuch bhi change krte h to vo original data ko bhi effect krta h.

Example:  Arrays are passed by reference ko samjne ke liye
Step 1) Same code ko copy kro apne editor me-
class arrayDemo {
   public static void passByReference(String abc[]){
     a[0] = "Changed";
   }
 
   public static void main(String arg[]){
      String []b={"Coding","Aliens","hi"};
      System.out.print("Before Function Call    "+b[0]);
      arrayDemo.passByReference(b);
      System.out.print("After Function Call    "+b[0]);
   }
}
Step 2) Save, Compile aur Run the code. 
Output:
Before Function Call    Coding
After Function Call    Changed

Multidimensional arrays

Multidimensional arrays are actually arrays of arrays.
 2D array, 3D array .. so on sare hi multi-dimensional array h.
Multidimensional array variable declaration example
int two_D[ ][ ] = new int[2][2] ;
Example
public class CodingAliens {
public static void main(String[] args) {

// Create 2-D array.
  int[][] two_D = new int[4][4];

  // Assign three elements in it.
  two_D[0][0] = 1;
  two_D[1][1] = 2;
  two_D[3][2] = 3;
  System.out.print(two_D[0][0] + " ");
}

}
Output:
1
#comment section me reason de ans 1 q aya ??

Audience

Humara Java programming tutorial sbhi  beginners or professionals ke liye design kiya gya h for easy to understand humne (HINDHI+ENGLISH) HINGLISH ka use kia h.Humaari intention apko padana ni educate krna h pad to aap khud hi loge.

Humne apko educate kiya ye post likh kr ap kisi or ki help kre educate krne me is post ko use share krke ,Or sath me aap humara page save bhi kr skte h new new  coding se related posts ke liye ctrl+D click krke .

                           Thank You .”HAPPY CODING” from the </CodingAliens>.   

Problem

 Hum apko  assure krte h ki is Java tutorial me koi mistake ni h.Fir bhi apko koi mistake ya problem lagti h to, please post the problem in the contact form.

Post a Comment

0 Comments