Pages

Arrays - Basic Java Tutorials For Selenium WebDriver

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.

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.

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.

13 comments:

I 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?

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.