Java for Selenium Part 6

Java Arrays, Java IO (Input in addition to Output)

i) Java Arrays

ii) Java IO (Input in addition to Output Operations)
---------------------------------------------
i) Java Arrays

What is Array?

In Java, Array is an Object that holds a fixed set out of values of a unmarried information type.

The length of an Array is established when the Array is created.

Array length is fixed, Index starts from Zero.

Declaration of Arrays

1st Method

Syntax:

dataType arrayName []; // Creating Array

arrayName = novel dataType[size]; //Define Size

arrayName [index0] = value; //Assign value 
arrayName [index1] = value; //Assign value 
.
.
---------------------------
Example:

int a [];//Create Array
a = novel int [3]; //Define Size
a[0]=10;
a[1]=20;
a[2]=30;

System.out.println(a[0] + a [1]);//30
System.out.println(a[2]);//30
---------------------------------------------
//Assign values to Array elements that to a greater extent than than the size of Array (Run-time Error)

int a [];//Create Array
a = novel int [3]; //Define Size
a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;

System.out.println(a[0] + a [1]);//30
System.out.println(a[2]);//30
---------------------------------------------
//Assign Values to about elements only.

int a [];//Create Array
a = novel int [4]; //Define Size
a[0]=10;
a[2]=30;

System.out.println(a[0] + a [2]);//30
-----------------------------------------
//Assign invalid values (data type) - Syntax Error

int a [];//Create Array
a = novel int [3]; //Define Size
a[0]=10;
a[1]=30;
a[2]=1.23;//Syntax Error
System.out.println(a[0] + a [1]);//30
---------------------------------------------
2nd Method

Syntax:

dataType [] arrayName = novel dataType [Size]; Declare Array amongst Size

arrayName [index] = value;

Example:

int [] a = novel int [3];// Declare Array amongst Size
a[0]=10;
a[1]=10;
a[2]=20;
System.out.println(a[0] + a[1]);//20
---------------------------------------------
3rd Method

Syntax:

dataType [] arrayName = {value1, value2, value3};

Example:

int [] a = {10, 20, 30, 40, 50};//Declare Array in addition to Assign Values

System.out.println(a[1] + a[2]);//50
---------------------------------------------
Difference betwixt Java in addition to VBScript inwards cases of Arrays.

VBScipt

Dim a(3)
a(0)=10
a(1) =20
a(2) =30

Or
Dim a(3)
a(0)=10 'Integer
a(1) ="India" 'String
a(2) =1.234 'Double Type

Java

int [] a= {10, 20, 30}; //Correct

int [] b = {10, 20, 1.2}; //Incorrect
-----------------------------------------------
Declaring unlike types of Arrays

char [] a = {'A', 'B', 'C', 'd'}; //Array of Characters
int [] b = {10, 20, 30, 40}; //Array of Integers
String [] c = {"UFT", "Selenium", "RFT", "LoadRunner"};//Array of Strings
boolean [] d = {true, true, false, true};//Array of Logical Values
double [] e = {1.234, 2.456, 6.7}; // Array of Decimal indicate values
System.out.println(a[2]);//C
System.out.println(c[1]);//Selenium
---------------------------------------------
Array Operations

1) Print an Array using for loop

int [] array1 = {1, 2, 3, 4, 5};

System.out.println(array1.length);//5

for (int i=0; i < array1.length; i++){
System.out.println(array1[i]);
--------------------------------------------------
2) Print an Array using Enhanced for loop

int [] array1 = {1, 2, 3, 4, 5};

System.out.println(array1.length);//5

for (int i: array1){
System.out.println(i);
---------------------------------------------
2) Copy values from 1 Array to another

int [] array1 = {1, 2, 3, 4, 5};
int [] array2 = array1;

System.out.println(array2.length);//5

for (int i=0; i < array2.length; i++){
System.out.println(array2[i]);

3) Copy detail value from 1 Array to another

int [] array1 = {1, 2, 3, 4, 5};
int [] array2 = {array1[2]};
System.out.println(array2[0]);//3
---------------------------------------------
Two Types of Arrays inwards Java

1) Single Dimensional Array

2) Multi Dimensional Array
--------------------------------------
Example:

int [] array1 = {1, 2, 3, 4, 5};//Single Dimensional Array
int [] [] array2 = {{1, 3, 5, 7}, {2, 4, 6, 8}};// Multi Dimensional Array

System.out.println(array1[2]);//3

System.out.println(array2[0][0]);//1
System.out.println(array2[1][0]);//2
System.out.println(array2[1][3]);//8
System.out.println(array2[0][2]); //5
---------------------------------------------
Advantages in addition to Disadvantages of Arrays

Advantages:

Using Arrays nosotros tin destination optimize the code, information tin destination endure retrived easily.

We tin destination cash inwards one's chips required information using index position.

Disadvantages:

We tin destination shop fixed set out of elements only.

It doesn't modify its size during execution.
---------------------------------------------
ii) Java IO (Input in addition to Output) Operations

There are 3 ways available for Reading input.

1) Scanner

2) DataInputStream

3) BuffuredReader
---------------------------------------------
Reading Data (Read using input devices, Read information from Files)
---------------------------------------------
Using java.util.Scanner is the easier means in addition to it includes many methods to chack input is valid to read.

Example:

Scanner scan = novel Scanner(System.in); //System.in is Input Stream

System.out.println("Enter Your Name");
String cite = scan.nextLine();
System.out.println(name);

System.out.println("Enter Your City");
String metropolis = scan.next();
System.out.println(city);

System.out.println("Enter Your Phone Number");
long phoneNumber = scan.nextLong();
System.out.println(phoneNumber);

System.out.println("Enter Your ID");
int id = scan.nextInt();
System.out.println(id);

System.out.println("Enter a Value");
double d = scan.nextDouble();
System.out.println(d);

System.out.println("Enter a Character");
char a = scan.next().charAt(0);
System.out.println(a);

System.out.println("Enter a Logical Value");
boolean b = scan.nextBoolean();
System.out.println(b);
---------------------------------------------
Display Output on the Console

System.out.println(a);//10 (Print Value of the Variable)
System.out.println("Welcome to Selenium");//Welcome to selenium (Print Message)
System.out.println("Addition of a, b is " + (a+b));//Print message amongst Variable value/Mathematical functioning output
System.out.println("Value of a is "+ a + " Value of b is "+ b);
---------------------------------------------

Sumber http://www.gcreddy.com/
Post a Comment (0)
Previous Post Next Post