What is Array?
As we have learnt 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 in java software development. 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 software development language. One Dimensional Array and Two Dimensional Array.
One Dimensional Array
Above given Image describes one dimensional array having one row of data. One dimensional array is just like spreadsheet with data in one row. As shown in above Image, Index of array Is starting from 0. Means value 10 has Index 0, 12 has Index 1 and so on and each value can be Identified by Its Index. 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 of java software development language. 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 in java software development language 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"}};
Explained very well
ReplyDeleteVERY GUD
ReplyDeleteI am not understanding this part. I have read the tutorial on For loops and thought i understood it. Can someone please explain what 'i' and 'j' stand for and what determines that the loop for 'i' will be iterated 3 times but the loop for 'j' will be looped for only two times?
ReplyDeletefor(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.
Here i and j are indexes. Above example is a two dimensional array of 3 rows and 2 columns. In order to iterate through all 3 rows the loops executes 3 times and for 2 columns it iterates 2 times.
ReplyDeletefor an array[3][2],
array[0][0]--row0column0--user1
array[0][1]--row0column1--password1
array[1][0]--row1column0--user2
array[1][1]--row1column1--password2
array[2][0]--row2column0--user3
array[2][1]--row2column1--password3
What value return in str[i].length syntax?
ReplyDeletestr[i].length will return 2
DeleteBecause in above example,
String str[][] = {{"User1","Password1"},{"User2","Password2"},{"User3","Password3"}};
str[i] represents each row and each row length i.e., str[0].length, str[1].length, str[2].length is 2.
Hope this helps. Please correct if my understanding is wrong.
Thanks u very much.
DeleteThanks u very much.
ReplyDeletethe result shown is not two dimensional:(
ReplyDeleteif we rewrite like
for(int i=0; i<str.length; i++)
{
for(int j=0; j<str[i].length; j++)
{
System.out.print(str[i][j]);
}
System.println(" ")
}
result in the console will be in 2 dimensional array
Aravind G! You are a blessing for learners!
ReplyDeleteHow can we pass the array into a webdriver selector?
ReplyDeleteFor eg. String str[] = { "Option1", "Option2" };
driver.findElement(By.cssSelector(str[])).click();
Will that be possible?
@anowar yes you can do that but like this
Deletedriver.findElement(By.cssSelector(str[0])).click();
instead of the thing you pass in " " you can simply write str[0] if its single dimension if its multi dimensional then you can write str[0][0]