ii) Java Basics for Selenium (Array)

 Array:

Java array is an object which contains elements of a similar data type. Additionally, The elements of an array are stored in a contiguous memory location. It is a data structure where we store similar elements. We can store only a fixed set of elements in a Java array.

  1. One dimensional

  2. Two dimensional

  3. Object array


  1.  One dimensional array:


Declaration Syntax:

 int j[ ]; // Declaring array

j= new int[5]; //Allocating memory to array

  Or

int[ ] j = new int[5]; // Combining both statement in one


Example

class OnedimensionalStandard

{

public static void main(String args[])

{    

int[ ] Mydata = new int[3];

Mydata[0] = 10;

Mydata[1] = 20;

Mydata[2] = 30;

//printing array  

for(int i = 0; i<Mydata.length; i++)

{

System.out.println("One dimensional array elements are");    

  System.out.println(a[i]);   

}

}



















    2. Two-dimensional array


Declaration Syntax:

Data_type[ ][ ] array_name = new data_type[x][y];

        For example: int[][] arr = new int[10][20];


Initialization Syntax:

array_name[row_index][column_index] = value;

        For example: arr[0][0] = 1;


Example

class TwoDimensional

{

public static void main(String args[])

{    

int[ ][ ] Mydata = new int[3][3];

int trow = Mydata.length;

int tcol = Mydata[0].length;

 

for(int i = 0; i<trow.length; i++){

for(int j=0; j<tcol.lenght; j++)

{

System.out.println("One dimensional array elements are");    

System.out.println(a[i][j]);   

System.out.println( );

}

}

}














  1. Object array


Declaration Syntax:

Object[ ][ ] array_name = new data_type[x][y];

        For example: Object[ ][ ] arr = new Object[10][20];


Initialization Syntax:

array_name[row_index][column_index] = value;

        For example: arr[0][0] = 1;


Example

class TwoDimensional

{

public static void main(String args[])

{    

Object[ ][ ] Mydata = newObject[3][3];

int trow = Mydata.length;

int tcol = Mydata[0].length;

 

for(int i = 0; i<trow.length; i++){

for(int j=0; j<tcol.lenght; j++)

{

System.out.println("One dimensional array elements are");    

System.out.println(a[i][j]);   

System.out.println( );

}

}

}


Comments

Popular posts from this blog

For Get Dynamic data & USe it into Postman Body parameters

API Automation Testing Script to check Response

Login through Data Driven Testing in selenium

Which dependencies needed in pom.xml while doing automation through selenium?

Download Java(JDK) & Eclipse IDE for selenium

What is data driven testing?

How to extract all the links in selenium?