Pages

Wednesday, 23 September 2015

Arrays -Selenium WebDriver

Arrays - Basic Java Tutorials For Selenium WebDriver

What is Array?

As we have learn in my post about DIFFERENT DATA TYPES, We can store values in variables based on type of data like int i=5; double d = 12.254; etc. Now if you wants to store multiple values (10 or 15 or more different values) in different variables then it will be overhead for you to create and initialize different variables for each value. In this situation, you can use array to store multiple different values in array. An array can store multiple value of same data type(int, char, String) at the same time and each stored data location has unique Index.

There are two types of array in java.
  1.  Dimensional Array and Dimensional Array.
  2.  One Dimensional Array
One dimensional array is just like spreadsheet with data in one row.  We can create and Initialize values In array as shown In bellow given example.
public class Array_Example {

 public static void main(String[] args) {
  int a[] = new int[6]; //Array declaration and Creation. 6 is length of array.
  a[0] = 10; //initialize 1st array element
  a[1] = 12; //initialize 2nd array element
  a[2] = 48; //initialize 3rd array element
  a[3] = 17; //initialize 4th array element
  a[4] = 5;  //initialize 5th array element
  a[5] = 49; //initialize 6th array element
 
  for(int i=0; i<a.length; i++){
  System.out.println(a[i]);
  }
 }

}

In above example, Length of array a[] is 6 and data type of array is int. Means we can store max 6 integer values in this array. for loop helps to print value of each array cell. a.length will return length of array. When you will run above example in eclipse, You will get bellow given result in console.
a[0] Holds 10
a[1] Holds 12
a[2] Holds 48
a[3] Holds 17
a[4] Holds 5
a[5] Holds 49

Another way of creating and initializing same one dimensional array is as shown bellow.
int a[] = {10,12,48,17,5,49};

Two Dimensional Array


Tow dimensional array is just like spreadsheet with multiple rows and columns having different data in each cell. Each cell will be Identified by it's unique row and column Index combination(Example str[5][3]). We can use two dimensional array to store user id and password of different users as shown in above Image. For above given example Image, We can create and initialize values in two dimensional array as shown in bellow given example.
public class Twodimarray {

 public static void main(String[] args) {
  String str[][] = new String[3][2]; //3 rows, 2 columns
  str[0][0]="User1";
  str[1][0]="User2";
  str[2][0]="User3";
  str[0][1]="Password1";
  str[1][1]="Password2";
  str[2][1]="Password3";
 
  for(int i=0; i<str.length; i++){//This for loop will be total executed 3 times.
   for(int j=0; j<str[i].length; j++){//This for loop will be executed for 2 time on every iteration.
    System.out.println(str[i][j]);
   }
  }
 }
}

When you will run this example in eclipse, It will return bellow given result in console.
User1
Password1
User2
Password2
User3
Password3

Other way of creating and initializing same two dimensional array is as shown bellow.
String str[][] = {{"User1","Password1"},{"User2","Password2"},{"User3","Password3"

No comments:

Post a Comment