Java Arrays and Java IO

Java Arrays and Java IO

i) Java Arrays

ii) Java IO (Input and Output Operations)

i) Java Arrays

What is Array?

In Java, Array is an Object that holds a fixed number of values of a single data 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 = new dataType[size]; //Define Size

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

int a [];//Create Array
a = new 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 more than the size of Array (Run-time Error)

int a [];//Create Array
a = new 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 some elements only.

int a [];//Create Array
a = new 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 = new 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 = new dataType [Size]; Declare Array with Size

arrayName [index] = value;

Example:

int [] a = new int [3];// Declare Array with 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 and Assign Values

System.out.println(a[1] + a[2]);//50
————————————-
Difference between Java and VBScript in 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 different 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 point 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 one 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 particular value from one Array to another

int [] array1 = {1, 2, 3, 4, 5};
int [] array2 = {array1[2]};
System.out.println(array2[0]);//3
———————————
Two Types of Arrays in 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 and Disadvantages of Arrays

Advantages:

Using Arrays we can optimize the code, data can be retried easily.

We can get required data using index position.

Disadvantages:

We can store fixed number of elements only.

It doesn’t change its size during execution.

Java Arrays and Java IO Operations

ii) Java IO (Input and Output) Operations

There are three ways available for Reading input.

1) Scanner

2) DataInputStream

3) BuffuredReader
———————-
Reading Data (Read using input devices, Read data from Files)
———————
Using java.util.Scanner is the easier way and it includes many methods to chack input is valid to read.

Example:

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

System.out.println(“Enter Your Name”);
String name = scan.nextLine();
System.out.println(name);

System.out.println(“Enter Your City”);
String city = 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 with Variable value/Mathematical operation output
System.out.println(“Value of a is “+ a + ” Value of b is “+ b);
—————————-

(223)

You may also like...

Leave a Reply