Arrays

 Used to store multiple values with the same data type in a single variable


Array is collection elements of same data type(Homogenios data).

We can store multiple values into a single variable.


2 types of arrays

---------

1) Single dimensional

2) two dimensional/multidimensional


Single dimensional

----------------------

1) Declare an array

2) insert values into array

3) Find size of an array

4) read single value from an array

5) read multiple values from an array

Examples:


import java.util.Arrays;

public class ArrayPrac {

public static void main(String[] args) {

//declare
--approach 1
int a[] = new int[5];
//insert
a[0]= 100;
a[1]= 200;
a[2]= 300;
a[3]= 400;
a[4]=500;
//approach 2
int []b= {111,222,333,444,555};
System.out.println(b.length);
//get single value from array
System.out.println(b[3]);

System.out.println(Arrays.toString(a));

for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
//enhanced for loop
for(int x:b){
System.out.println(x);
}

System.out.println("---MULTI------");

int bcc[][]= new int [3][2];
bcc[0][0]=100;
bcc[0][1]=100;
bcc[1][0]=100;
bcc[1][1]=100;
bcc[2][0]=100;
bcc[2][1]=100;

int cc[][]= {{12,23,44},{34,45,55},{56,67,66},{78,89,77}};

System.out.println("rows are " +cc.length);
System.out.println("columns are "+ cc[0].length);

for (int r = 0; r < cc.length; r++) {
for (int c = 0; c < cc[r].length; c++) {
System.out.print(""+ cc[r][c]+" ");
}
System.out.println();
}
System.out.println("Enhanced for loop");
for (int x[]:cc) {
for (int vv:x){
System.out.print(vv+" ");
}
System.out.println( " ");
}

Object aaa[] = new Object[4];
aaa[0] ="Sudheer";
aaa[1]="Myra";
aaa[2]="Nirvana";
aaa[3]="bindu";

Object []bbb= {"Sudheer","Chittireddy","Myra Nirvana Chittireddy","Bindu"};
for (int i = 0 ;i<bbb.length;i++){
System.out.println(bbb[i]);
}


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

for (Object gg:bbb){
// System.out.println(gg);
if(gg.equals("Sudheer")){
System.out.println("Caught Sudheer");
} else if (gg.equals("Chittireddy")) {
System.out.println("Chittireddy Family coming into finals");

}else {
System.out.println("Out of loop");
}

}
}
}


Comments

Popular posts from this blog

Implicit and Explicit Waits,FluentWait,PageLoadTimeOut

A Interview Questions- selenium