Session 6- Working with Java Arrays | Single & two dime… — Transcript

Learn how to work with single and two-dimensional Java arrays, including declaration, indexing, and usage in Selenium automation.

Key Takeaways

  • Arrays store multiple values of the same data type in a single variable.
  • Array indices start at zero and go up to size minus one.
  • Using arrays simplifies managing and operating on large sets of data.
  • Arrays are crucial for efficient Java programming and Selenium automation.
  • The 'new' keyword allocates memory for array elements.

Summary

  • Introduction to arrays as a derived data type in Java that can store multiple values of the same data type.
  • Explanation of primitive vs derived data types and the need for arrays to manage multiple values efficiently.
  • Definition of arrays as collections of homogeneous data elements.
  • Use case example: storing 100 student IDs in one array instead of 100 separate variables.
  • Syntax for declaring and initializing arrays using the 'new' keyword and specifying size.
  • Memory allocation for arrays with multiple indexed locations starting from zero.
  • Explanation of array indexing starting at zero and maximum index being size minus one.
  • Importance of arrays for better data management and easier operations on multiple values.
  • Basic operations on arrays including storing and retrieving data using index positions.
  • Clarification on why arrays are essential for handling large data sets in Java programming and Selenium testing.

Full Transcript — Download SRT & Markdown

00:03
Speaker A
So in the previous class, we have seen some basics like how to work with the data types and variables, Java variables and data types. And then we have seen how to use conditional statements and control statements, looping statements, right? So today we'll see one more topic: arrays. So most important topic, please listen to this very carefully. So what is an array? In the last class, we have seen like there are two kinds of data types: one is primitive and derived, right? So what is primitive data type? Primitive means we can store only one value at a time in one variable. So one variable allows us to store only one single value at a time, whereas derived data types can store multiple values into one single variable. So arrays are also one of the examples of derived data types. So if I create any variable with array type, you can store multiple values.
00:19
Speaker A
topic arrays so most important topic uh please listen this very carefully so what is an array in the last class uh uh we have seen like uh there are two kinds of data types one is primitive and derived right so what is primitive data type primitive means we can store only one value at
00:46
Speaker A
Okay, so first of all, let's see what is an array and how can we declare an array. And after that, we'll see different types of array-related operations. Okay, so first, what is an array? What is the definition? So array is a collection of elements of, say, name data type. So that's the basic definition. Array is a collection of... Guys, can you see my screen? Everyone, can you see my screen? Okay, great. So array is a collection of elements. Elements in the sense some values, set of values of same data types. And same data type in the sense which is called homogeneous data. So we have two kinds of data: homogeneous and heterogeneous. Homogeneous means all the data belongs to the same data type. Heterogeneous means the data belongs to different data types. So array is a variable which can store multiple values of the same data type.
01:04
Speaker A
store multiple values okay so first of all let's see what is an array and how can we declare an array and after that we'll see different type of array related operations okay so first what is an array what is the definition so array is a collection of elements of say name data
01:31
Speaker A
Okay, and we can store multiple values into a single variable. So when is it required? So when do we have to use the array concept? One example: let us say in a class there are 100 students studying, and I want to maintain 100 students' data. Let's say every student has a name, every student has IDs, and so on. Now I want to maintain 100 student IDs. Every student has a unique ID, so I want to store 100 student IDs. Normally, how many variables do we have to create? 100 variables are required. 100 variables are required to store 100 student IDs. For every student, there is one specific variable required. So if we create 100 variables, there are two problems. One is we cannot manage those number of variables; it is very difficult to manage. And also, it is very difficult to operate. Suppose based on these values, if you want to perform any operations tomorrow, it is very difficult to perform operations on every student ID or every student's some value.
01:56
Speaker A
called homogeneous data so we have two kinds of data homogeneous and heterogeneous homogeneous means all the data is belongs to same data type heterogeneous means the data is belongs to different data types so array is a variable and which can store multiple values of same data
02:18
Speaker A
So this is the main problem if you have more number of variables in your script. So instead of doing that, we can simply create one variable, and that can hold 100 values or more than 100, whatever you have. So any number of values we can store in one single variable. So that is called array. Array is a collection of elements or set of values of the same data type. Same data type means arrays allow only homogeneous data, right? So how can we declare an array variable? So if it is a normal variable, we can say data type variable name, and we can directly assign data like this. So int is a data type, A is a variable, 10 is a value which we have assigned to the variable.
02:39
Speaker A
having IDs and so on now I want to maintain 100 student IDs every student is having unique ID so I want to store 100 student IDs and normally how many variables we have to create 100 variables are required 100 variables are required to store 100 student IDs for every student there is one
03:00
Speaker A
And suppose if it is an array variable, so array variable means which can store multiple values, right? So how can we define an array variable? First data type, and then variable name, and one single bracket we have to keep like this, single bracket equal to, we have to use something called new keyword, new. Okay, this particular keyword will allocate memory to this variable. New, and again data type one more time, int. In the one bracket here, we can specify how many values you want to store in this variable. Let us say I want to store 10 values or five values, I can specify that. And this is called as number of values or number of locations, we can simply say. And how many values we want to store in array, that we can specify in the bracket.
03:19
Speaker A
it is very difficult to perform operations on every student IDE or every student some value so this is a main problem if you have more number of variables in your uh script so instead of doing that we can simply create one variable and that can hold 100 values or more than 100 whatever you
03:39
Speaker A
So this is the syntax: the variable name, single bracket equal to new int, again same data type, in the bracket we can specify how many number of values we want to store. So as soon as you declare the variable like this, it will allocate five locations to this particular variable. Normally, it will allow only one location if it is a normal variable because it is a variable. So that it will say five different locations, okay, five different locations.
03:59
Speaker A
can say data type variable name and we can directly assign data like this so int is a data type A is a variable 10 is a value which we have assigned to the variable and suppose if it is an array variable so array variable means which can store multiple values right so how
04:18
Speaker A
But how can we store the data in this array? So to store the data in array, first it is allocated five rows. Actually, this number is representing number of rows, so I can say this is five rows will be allocated like this: 1, 2, 3, 4, and five. So five locations are allocated. The variable name is A. Now these are the five locations: 1, 2, 3, 4, 5. So it depends upon the number you specified here. So those many number of locations will be allocated. Now every location is represented by an index number. Every location needs some address, right? If you want to operate this data, if you want to add some data in the location or if you want to retrieve some data from that particular position, we need some kind of address. That is called index. So every location is identified by using index, and here in array, index number will start from zero.
04:40
Speaker A
in the one bracket here we can specify how many values you want to store in this variable let us say I want to store 10 values or five values I can specify that and this is called as uh number
04:53
Speaker A
So this is a zero position, first position, second position, third position, and then fourth position. So total number of elements are five, and what is the maximum index here? Four. That means if maximum number of elements are n, the maximum index should be always n minus one because index is always starting from zero. Okay, zero is also one of the locations. So here also we can store the data. So if number of values are five, then obviously the last index value should be always n minus one, that is four. Okay, remember this.
05:12
Speaker A
to store so as soon as you declare the variable like this it will allocate five locations to this particular variable normally it will allow only one location if it is a normal variable because it is a AR variable so that it will say five different locations okay five different locations
05:34
Speaker A
Now how can we access? For example, here I want to store some data. Let's say I want to store something called 100. Then how can we store? Here is simple: A of zero is equal to 100. So zero is a position or the index of the element. It is equal to 100. So now this 100 will store into the zero position. And similarly, suppose in the first position I want to store some other value. So let's say A of 1 is equal to 200. Like this, we can store values. A of 2 is equal to 300. So like this, for every index number, we can store some value. If you're not storing any value, still that memory is empty.
05:46
Speaker A
allocated like this 1 2 3 4 and five so five locations are allocated the variable name is a now these are the five locations 1 2 3 4 5 so it can depends upon the number you specified here so
06:01
Speaker A
Okay, so later anytime you can store the value. Until you store the value, that space is empty. And the last one, A of 4 equal to 400 is the last position. And if you're trying to add value in the fifth position, it will give you exception: array index out of bound exception because there is no fifth position here. Four is the last position. So if you're trying to add some more value in the fifth position, it will give you array index out of bound exception. Okay, so this is how internally the array will be managed. And at the time of retrieving the data also, we want to get the value from the first place. So you can see A of 1, you can put this in the print statement. So this will get the value from the particular position.
06:20
Speaker A
we need some kind of address that is called index so every location is identified by using index and here in Array index X number will start from zero so this is a zero position first position second position third position and then fourth position so total number of elements are five and what is
06:43
Speaker A
Or if you want to get the value from the fourth position, so A of 4. So that you can put in the println statement. So this will extract the value from this array. So by using index number, we can identify or we can represent each and every value in arrays. And the ultimate goal here is what? We can store multiple values or group of values into one single variable. Okay, and all those values should be same data type. Homogeneous data means what? Same type of data we have to add. So one is integer, one is float, another one is character, another one is string. This is not possible directly. It is not possible. But there are ways to store multiple data types also. I will tell you at the end of the session. But by default, as per array concept, it is designed only for homogeneous data. Okay, but indirectly we can also store heterogeneous data by creating o...
07:04
Speaker A
last index value should be always minus one that is four okay remember this now how can we access for example here I want to store some data let's say I want to store something called 100 then how can we store here is simple a of Zer so zero is a position or the index of the element
07:24
Speaker A
is equal to 100 so now this 100 will store into the zero position and similarly suppose in the first position I want to store some other value so let's say a of 1 is equal to 200 like this we
07:38
Speaker A
can store values a of 2 = to 300 so like this for in every index number we can store some value if you're not storing any value still that memory is empty okay so later anytime you can store
07:51
Speaker A
the value until unless you store the value that space is empty and the last one a of four equal to 400 is the last position and if you're trying to add value in the fifth position it will give
08:04
Speaker A
you exception array index out of bound exception because there is no fifth position here four is a last position so if you're trying to add some more value in the fifth position it will give you array index out of bond exception okay so this is how internally the array will be managed and at the
08:24
Speaker A
time of retrieving the data also we want to get the value from first place so you can see a of one you can put this in the print statement so this will get the value from the particular position
08:35
Speaker A
or if you want to get the value from the fourth position so a of four so that you can put in the print L statement so this will extract the value from this erring so by using index number we can
08:48
Speaker A
identify or we can represent each and every value in arrays and the ultimate goal is here is what we can store multiple values or group of values into into one single variable okay and all those values should be same data type homogeneous data means what same type of data we have to
09:10
Speaker A
add so one is integer one is float another one is character another one is string this is not possible directly it is not possible but there is a ways to store multiple data types also I will tell you at the end of the session but by default as per array concept it is designed only for home
09:29
Speaker A
homogeneous data okay but indirectly we can also store heterogeneous data by creating object type of okay that is different concept we'll discuss later so this is a basic initial understanding so how we can create an array how can we store the values in Array how can we operate by using index
09:49
Speaker A
index will play most important role in arrays concept okay now there are two kinds of arrays we have one is single dimensional and second is two dimensional single dimensional and then two dimensional single dimensional two Dimension which is also called multi-dimensional so first
10:12
Speaker A
let us start with the single dimensional once you understand then we go with the two dimensional okay so first let us talk about single dimensional so what is single dimensional is just now we have discussed that is comes under single Dimension means only we have a rows in Array we can create
10:29
Speaker A
only row that is single dimensional and along with the rows if you create columns also that is called multi-dimensional or two dimensional rows and columns both will be there okay so first let us start with the single dimensional in the single dimensional array we will do different type
10:50
Speaker A
of operations we can do any number of operations first of all let's start with the basic types of operations basic operations the first thing how to declare an array and how to add values into array and suppose if I already added some values into array so how many values we have added so that
11:09
Speaker A
also we can find that is size of an array we can find and how to read a specific value let's say have stored 100 values in Array and how can we read a single or specific value from array and
11:22
Speaker A
how can we read multiple values from ARR so these are the basic operations later we will discuss some more operations but these are the very basic until unless you understand this you cannot do further operations so declaration adding values into array finding size of an
11:38
Speaker A
array and then read single value from array read multiple values from array okay now first let us start with declaration and everything so directly go to eclipse and uh let's go to our project so this is our project earlier we already created now let's create cre a new package for today new
12:02
Speaker A
package this is day six today click on finish okay now inside this I'm creating a new class so I'll name it as single dimensional area and here we should take main method also click on the p all right so now it is created a new class and inside this there
12:33
Speaker A
is a main method now inside this main method we have to write our script so first single Dimension so what's the first thing so let me put all these points just keeping this comment here so now we'll see one by one so first thing how to
12:54
Speaker A
declare an array so to declare an array there are two different approaches are there okay so the first approach the Declaration part okay declaring arrays so the first part is let's say approach one in approach one so first we will create an array variable let's say in a bracket equal to new in
13:24
Speaker A
bracket and here we can specify how many values we want to store in a array let's say five values I want to store and then semicolon this is called declaration and once you declare an array then we can store the data into the array we can add values into array so to add values into array we
13:43
Speaker A
have to represent each and every location so when I create an array it will store it will create five different rows let say this is my array okay 1 2 3 4 five locations 0 1 2 3 and four four and
14:00
Speaker A
all are integers So currently I'm storing some numbers here like this I want to store 300 400 and then okay so how to store the data in an array so to store the data in Array we can simply say
14:14
Speaker A
this is just a declaration once we declare an array then we can store the data or we can add the data so how can we do this by using index a of zero in a zero position what is the value you
14:26
Speaker A
want to store 100 so that you can specify that's it now a of 1 and 200 and a of three 300 a of 0 1 2 and this is three 400 a of four is 500 okay so like this we can store values into array so
14:55
Speaker A
last index is always four here because we are specifying five is a number of locations and maximum index is four only and we can store even strings also instead of int if you create a string array you can store only strings and if you create if you create a character array you can store only
15:16
Speaker A
characters and if you create a double type of an array you can store only double values okay yes arrays always start from zero so index is zero every time remember this part point so zero is a starting point of index index will always start from zero as per the programming not only Java in
15:36
Speaker A
almost in every programming indexes will always count from zero itself okay so now this is the Declaration and then we have assigned some data into the array so now this array contains the data this is one approach so declaration and then assignment so declaration means we just
15:56
Speaker A
declar the variable assignment means we have ADD added or we have assigned some data into the variable this is one approach and the second approach is also there and I'll tell you which approach is preferable first let us understand what are the approaches now the second approach
16:16
Speaker A
approach two and we can Al we can declare and assign the data in one single line that is the second approach within single line we can create an array we can assign directly data into array for example here in variable name bracket single bracket equal to curly braces in the curly braces
16:41
Speaker A
we can directly add data like this 100 comma 200 comma 300 comma 400 like this you can directly keep adding n number of values you can also add a number of values like this so this is another approach declaration as well as assignment both are happened within single so this is another
17:01
Speaker A
approach this is first approach and this second approach so but which one we have to choose when we have to go with the first approach when we have to go with the second approach if you look at both of the things second approach seems very simple and very easy right and within single line
17:15
Speaker A
of code we can do it but sometimes we have to go with the first approach sometimes and most of the times we can also prefer second approach but there is a criteria so what is the criteria sometimes what happens is if you already know how many elements you want to store or how much
17:35
Speaker A
of data you want to store in an array if you already know that information and you don't want to add more values in future you don't want to add any more values in future in Array then you can go with the first outut this is fixed the size is fixed when I say five here only
17:55
Speaker A
five elements you can store you cannot store more than that so this is fixed okay so if you already know how many values you want to store and in future if you don't have any plan to add any
18:07
Speaker A
more values into array then you can go with this approach first approach okay and suppose if you don't know if you don't know how many values you want to store maybe in future also if you want to add more number of values is there any possibility okay if there is any possibility to
18:25
Speaker A
add more number of values in future and you don't know exact number how how many values you want to store in those cases approach two is preferable okay this is more dynamic because we can add more number of values and if you don't want values you can remove the values also so it is more dynamic
18:43
Speaker A
in nature so we can prefer the first approach or we can prefer the second approach if you already know size of an array and if you don't want to add more values in future then go with the first approach and if you don't know how many values you want to add Maybe in future also you
18:59
Speaker A
can add number of values then you can go with the second approach okay these are the two different approaches which we have to declare an array you can go with this one or you can go with this one
19:11
Speaker A
fine now we have seen declaration and also adding values parall so this is called declaration and this is called adding values in the second example this is the Declaration and here we don't need new keyword okay so how many values we have added accordingly the locations also will be
19:28
Speaker A
automatically increased so if you want to update in future just add one more value here 500 or 600 you can just add one more value like this okay and here also index works okay this is a zero Index this is the first index second index third index fourth index so automatically as soon as
19:48
Speaker A
you are added more number of values automatically it will allocate locations okay so this is more dynamic in nature so you can add more number of values and you can also decrease number of values okay so for example here one problem with the we have with the first approach suppose you say five
20:10
Speaker A
locations defined five locations are allocated but if you added only three numbers and rest of them what happens those memory will be wasted still because we haven't asend any data into this until unless you asend the data the locations will be wasted right but in the second approach there
20:29
Speaker A
is no problem with that because if you have only two numbers only two locations are allocated if you have added five locations five locations are allocated even if you future if you remove two values or three values automatically the locations will be removed automatically right so this is how
20:46
Speaker A
this is mostly preferable because in future you can add more values or if you don't want you can REM existing values still index is working you can retrieve the data you can add more data so what whatever operations we can do in this same thing we can also do in the second approach okay
21:03
Speaker A
so these are the two different approaches which we have to declare and adding values now suppose I have already added some values into array then I want to find size how much size how many values we have added to the array that is basically called length of an array find instead of size we can say
21:24
Speaker A
length Okay find length of an array length means how how many values we have stored in Array so that information we can get so how to find it find length of an so the Declaration can be anything you can go with this approach or this approach no matter rest of the things will
21:48
Speaker A
continue as usual so find length of an array so to find the length of an array first we specify the array variable dot there is a keyword available here called Len this is a keyword and this will actually Returns the length of a array length means how many values we have
22:08
Speaker A
stored in an array but we have to print that value so to print that value you can keep this inside the printer system printer L A do length so whenever you want to find length of an array you can use this command a do length a is nothing but a variable do length is a keyword which will
22:30
Speaker A
find length of a string so when I run this this will give you how many values we have stored how many values we have stored five values we have stored so how many values we have stored those many number of values will be written that's called length of an array so a do length length
22:52
Speaker A
is a keyword through which we can find length of a an array so we can also put some description can say length of a okay so we can just concatenation operator because we want to join those two things so when you run this you will get exactly length of is five okay this
23:21
Speaker A
is one operation now suppose I want to read a specific value from I want to read need a value from array specific value from array so then how can we read so the next operation how to read value or data from AR specific location okay simple system. here
23:50
Speaker A
specify the location from which location you want value so for example here fourth location I want this value four five 00 I want so I can simply say four index number so here four is what four is index number four is index so if I specify four here a of four means 500 should return so
24:14
Speaker A
when I execute this you will get a 500 so if you want to extract a specific data from an array you can write like this okay for example I have 100 values in Array I want to retrieve all 100 values then how many print statements are required 100 print statements are required
24:35
Speaker A
right in every print statement we have to change the index number 0 1 2 3 like this it is very dous task right if you want to write 100 print statements it is a very tedious task so if you
24:48
Speaker A
want to read all the values if you want to read all the values all the data from array you can read it by using looping statement okay we can read it by using looping statement why looping statement is required so why looping statement is required a simple example I'll tell you so how to
25:10
Speaker A
extract the zero position a of zero system. out. pintn a of Z next statement system. Pinn a of1 again system. pintn a of so like this we have to write a number of statements we have 100 numbers 100 statements we have to write instead of doing that we will write only one single statement and
25:34
Speaker A
this statement will repeat multiple times in every iteration we will just try to increase this index number okay zero next round one next round two next round three so then what will happen every time you will get a a new value a of zero value in the first iteration second iteration a of one
25:51
Speaker A
third iteration a of two like this how many times we have to repeat this depends on the number of values depends upon the length of an array right so single statement we will repeat multiple times just by changing the index number by incrementing the index number and how many times we have to
26:12
Speaker A
repeat this till we reaches the last element till we reaches the length of an array okay so now tell me what is the starting point whenever you want to use a looping statement we have to know three things right what is the starting point initial value here index initial value
26:30
Speaker A
zero and how much value have to increment here index every time one means plus plus and where we have to stop as it is reached length right how many values are there so there we have to stop right so now let us try to understand if you don't put anything in a of bracket there are no
26:53
Speaker A
values here still will be created but there are no values that is normally called empty array if there are no values there is no use right so that is called empty array if you put the values values will be present if you're not putting the values simple just array will be created which is empty
27:13
Speaker A
and then if you find length of an array it will give zero itself we don't have any values in a a do length will return zero because there are no values in Array okay now so let us see how can
27:25
Speaker A
we read all the data from array So currently we have these many datas so all the data so simple example I'll tell you this let me just comment this if you don't have any values in then what will happen so you can say of bracket and here I'm not storing any data just you can simply declare
27:43
Speaker A
like this it is giving an error first of all the local variable a may not have been initialized because it is not enough we have to initialize an array initialization means at least we have to write new in how many values we have or we can Le to specify like this no values in Array okay
28:02
Speaker A
there are no values now what would be the length of an array here is it is giving ARR index of to one solution because so far we haven't store any values here you can see this length of enr is what
28:15
Speaker A
zero and here this statement is giving this index Auto Bond exception actually because we haven't store any value so far so there is no value in the fourth position so this statement is giving array index at Auto solution but if you find if you run as a Java application now you can see length is
28:33
Speaker A
zero because there are no values in this length is zero okay but arrays got created but there are no values there is no positions okay now I can say only 10 and 20 only two values I have added now if
28:48
Speaker A
you find the length of an array it will be two so two elements are there okay so like this we can create an array with some variable and if you create an empty array there are no values at all
29:01
Speaker A
but still array will be present okay now so how to read multiple values let me enable the statement so how to read multiple values means all the values from array I want to read by using index okay so now let's see the last reading all the values from array okay reading all all the values
29:28
Speaker A
from so how to read all the values from here we want to use a looping statement so instead of writing the statement multiple times just we will write only one time and we will keep changing the index number so which Loop we will prefer here is most of the times we use for Loop only especially
29:52
Speaker A
when you're working with ar is we use for Loop and there is another uh type of f Loop specifically designed for arrays or collections that is called for each Loop that is another flavor of normal F Loop okay so most of the times we use it I will show you both of the F Loops to read the data from
30:15
Speaker A
array so first we will see normal F Loop which we have already discussed in the previous session okay how to read the data by using normal F just normal or classic f so how to write for Loop okay
30:30
Speaker A
now we need to specify three things initialization condition and then incrementation right so what is initialization int I equal to I is representing index number starting from zero and how many times we have to repeat this how many times less than or equal to how many times we have to repeat
30:50
Speaker A
this it depends on number of values it depends on number of values how many values currently we we have 1 2 3 4 five values currently we have can you specify five here can you specify five no right because there is no value in the fifth position always index start from zero right so if
31:16
Speaker A
index start from zero this should be always I less than or equal to four this is right condition or you can put I less than five this is also right condition because whenever I value becomes five five less than five condition become false then it will automatically exit right whenever I value
31:39
Speaker A
become five five less than five condition become false then it will exit this is also right or if you want to specify maximum index you can say I less than or equal to if you want to specify index last index number you have to specify this is this clear everyone so simply I'm saying if
31:59
Speaker A
you say I less than or equal to Z if you want to use this should be last index number that is total length minus one total length minus one okay and if you're using less than then it should
32:17
Speaker A
be total length Okay for example here I'm saying four and five four or five suppose if you don't know length of a array there maybe 50 or there maybe 100 or 200 you don't know you don't know the size but how can we put this condition if you don't know the size how can we put this condition
32:39
Speaker A
if you don't know the size but I want to repeat this Loop for all the elements how much data we have in this array those many times we have to repeat this for Loop but you don't know how much
32:50
Speaker A
size it is if you know the size you can directly specify here you can say I less than or equal 4 okay or I less than five you can specify but if you don't know the size then how can we do this
33:03
Speaker A
yes you can get the length of an array dynamically how can we get length a DOT length Okay this is how we can put the condition then how much value we have to increase by value one so if you use
33:18
Speaker A
less than here directly can say a do length that will return the total number of elements and if you're using I less than or equal to then here also you should do minus one okay a do length minus one so is this clear to everyone please confirm in the chat box the condition is clear
33:39
Speaker A
everyone how to put this condition because why all this this because index will start from zero accordingly we have to arrange the condition okay because index will start from zero so accordingly we have to specify the condition if you are using only less than then a do length is enough okay
34:01
Speaker A
because we are seeing less than here to say I less than or equal to then a do length minus one also required okay byus one one is required why it is required because a do length will give total size
34:18
Speaker A
five it will return return but there is no element in the five so minus one will give the last index okay so remember this point so say a do length means you should say only less than equal to
34:32
Speaker A
not needed right so here actually you can put condition I less than or equal to 4 or I less than 5 same thing you can write I less than or equal to a DOT length minus one or I less than
34:54
Speaker A
a do length okay okay I'm writing all combinations you guys can try this later so all are correct all these conditions are correct right fine so now we put this next now we want to extract the data from the particular location so we can use one print statement a of here we specify the index
35:17
Speaker A
whichever index we pass that value it will return then that value will be printed and here instead of hard coding that value like 0 1 2 3 4 we can simply pass I value okay so then what will happen
35:32
Speaker A
first I value zero condition is true then it will enter into the block so initially a value of I a is I I value is what zero a of zero means what 100 so first time it will print the first value zero
35:47
Speaker A
value which is 100 now second iteration I value becomes one again condition is true now here a of one means 200 next I value becomes three so value will be 300 next to 400 next to 500 then as soon as I value becomes five five less than or equal to a do length condition become false
36:12
Speaker A
then it will exit from the F Loop so this is how we can simply write one F Loop which will read the data from array now we can see these are the values which we are able to read okay
36:26
Speaker A
this is by using normal F so instead of writing this statement multiple times we can write only one single time and we just changing the index number every iteration we are incrementing the I value by one so that we will able to capture all the values from array this is a way we can read
36:49
Speaker A
data from single dimensional array by using normal F Loop now let me show you another fun Loop which is called enhanced for Loop or for each loop it's more simpler than this one okay very simple so let me show you this is called enhanced F Loop also called as for each Loop this is another version
37:16
Speaker A
of loop mostly we use this with arrays concept and then collections also now let me show you same thing so how to read the data from array by using enhan follow so let's comment this now it's very simple first let me write the syntax and then I'll show you how we can execute this for same
37:38
Speaker A
but here we don't have initialization we don't have condition we don't have incrementation so we don't need to specify them so that is another beauty of enhancer for so initialization we will specify but incrementation will automatically happen condition is also Al inclusive inverse
37:59
Speaker A
so how we can do it just specify some variable name some X colon array name that's it and here just print x value over simple there is no index there is no incrementation there is no Condition Nothing is there simple these three statements are able to print all the data it is more simpler than
38:25
Speaker A
first one but how it is going to execute so that we have to understand very simple so a is an array which is having five different values right so let's say this is my array which is having five
38:39
Speaker A
values here 100 200 300 400 and then 5 here we don't need to bother about indexes okay when I use enhanced for Loop we don't need to bother about indexes so how it is going to execute in the a a is an array in the a what is the first value 100 100 so the first value from a will
38:59
Speaker A
be stored in X that is 100 first value is 100 so X is 100 then it will come inside then it will print 100 here in the next round it will get another number second number is 200 that will assign
39:14
Speaker A
to the X now it will come inside then x value 200 is printed third round it will assign 300 now x value becomes 300 now it come inside 300 is got printed so like this every time it will
39:29
Speaker A
capture the new number from array and store into a variable that we are got printing but when it will stop as soon as all elements are got completed or finished then this will automatically exit so this is how it will work simple and here we should use something called colon not equal to
39:51
Speaker A
colon we have to use as for the syex okay so in every round of iteration it will get each value from array and assign into variable first 100 will assign next 200 will assign to the ex next to 300 next 400 next 500 there are no more values then it will automatically stop so every
40:09
Speaker A
time whenever you get the value X immediately we are trying to print this value okay so this is how enhanced F Loop works so here we don't need to specify any condition no any realization no incrementation everything will be taken care of loop itself but in case of normal F we have to
40:29
Speaker A
specify index number condition all those things okay so when I execute this observe it will just print all the values as it is so which one is easiest one guys which one easiest one how you are feeling about normal for Loop and enhanced for Loop first one or second which one you
40:51
Speaker A
will prefer normal yes second one is more simpler because we are not going to deal with any indexes things right so most of the times we use enhanced for Loop or for each Loop when you're working with aray Concept in future even collections like array list hashmap set collection so almost every
41:11
Speaker A
collection we try to use this looping statement enhancers for Loop this is especially designed for collections and array concept okay so this is all about single dimensional array so we will go we going to discuss so many examples because this is most important topics and tomorrow also
41:32
Speaker A
the same topic will be continued and uh we will see more examples on this these are the very basic operations declaration adding values find length of an array reading single value multiple values like this if you know these operations then we will go to the next level like how can we sort
41:50
Speaker A
array how can we search an element in Array okay how can we add number of elements in Array how can we add total some so there are different type of programs we can practice based on the array itself these are the basic operations okay so if you want to print particular value don't go with
42:09
Speaker A
the looping statement okay so if you want to print a particular value we should not use any looping statement so directly you can print like this you can just directly use one single statement and specify the index of the number that's it we are not going to prefer any Lo Loop is what
42:27
Speaker A
what what is the purpose of the looping statement is repetion right if there is any something which are repeating multiple times then we will always prefer the looping statement so if you want to extract a particular value single value then there is no looping statement required you can directly
42:42
Speaker A
write a print statement by specifying the index number and whichever value you want to extract you can extract just by specifying the index number but when we have to use looping statement if you want to read all the data all the values from arrays from every index then we will prefer
43:00
Speaker A
the looping statement okay now this is a single dimensional array so the tomorrow session we will see more examples uh now let us discuss about two dimensional array so two Dimension ARR how we can use two Dimension array so this is another type of an array in the two dimensional array also we
43:24
Speaker A
can do the same kind of operation but first of all we'll see how to create two dimensional array two dimensional so two Dimension are and single dimensional what is a major difference if it is a single dimensional array we have only rows like this zero Row first row second row third
43:53
Speaker A
fourth this index is is basically representing the row number okay this is single dimensional array and two dimensional or multi-dimensional means we have a rows along with the columns like this like this so these are the rows and these are the columns columns and rows rows will start from
44:14
Speaker A
zero this is zero Row first row second row third row similarly column index also start from zero this is zero column First Column second column and so on this is called two-dimensional simple if you simply say is a tabular format rows and as well as columns rows index will start from
44:33
Speaker A
zero even column index also start from zero but how can we operate the data how can we add the data how can we retrieve the data Again by using index for example in this I want to store 100 in
44:47
Speaker A
the in this particular position so what is the row number of this zero What's the row column number of this zero so how to represent this location is a of 0 and 0 equal to 100 now this 100 will be stored in the zero row this is row this is a column first bracket we specify the
45:08
Speaker A
row number second bracket we specify the column number so this number will go and store in the zero row and zero column so here it will store similarly here I want to store some value the row number is three column number is two so a of three and two like this so in the third row
45:29
Speaker A
second column the 500 will be added at the same time retrieval also if you print the statement in the print Ln then this will return the value which is there in the z0 position same thing if you put this in the print L and this will return the value which is there in the third and third
45:46
Speaker A
row and second column okay so this is called two dimensional array so we have multiple rows and also we can have multiple columns also n number of rows and N number of columns there is no limit you can put n number of rows and N number of columns okay this is single and twood
46:03
Speaker A
dimensional array the difference between single and two dimensional single dimensional we have only rows whereas in two dimensional we have rows as well as columns okay now we'll see how to perform different type of operations in two dimensional array so almost the same operations
46:23
Speaker A
we are going to do whatever operations we do in the single dimensional same type of operations we can also do in two dimensional are so now we see how to declare how to add values how to find size here size in the sense not only rows column size also we can find and how to read a specific
46:44
Speaker A
or single value and how to read all the values from two dimensional array now we'll see one by one so I'll create another example new new class this called two or multi-dimensional array two dimensional taking main method saf okay so listen this very carefully little bit complicated uh just listen very simple you
47:20
Speaker A
need to more focus on this so first thing how to declare an array again there are two different approaches here just like a single dimensional array we have seen two different approaches right what is uh standard other one is dynamic one is fixed array we can say this
47:38
Speaker A
is a fixed and this is a dynamic so fixed means what first we have to declare an array then we can assign the data and here we can declare array immediately we can assign the data in the curly
47:50
Speaker A
braces same two approaches are applicable for two dimensional array also so declaring array okay so approach one first approach so first declaration in a instead of one bracket we say two brackets and equal to New Again data type and again two brackets and in the first bracket we
48:25
Speaker A
specify number of rows in the second bracket we specify number of columns okay so for example I want to have three columns three rows and then uh two columns okay three rows and two columns you can have n number of rows and number of columns you can have 10 columns 20 columns also no problem
48:46
Speaker A
so it depends upon the data we can specify the total number of rows and total number of column so this is called declaration so then what will happen if you create an array like this it it will allocate three rows and two columns so the array will be created like this three rows and two
49:05
Speaker A
columns zero Row first row second row zero column First Column always index start from zero only so that we have to always remember okay now I want to store some data here let's say here 100 20000 300 4 5 and then 600 so so to store the data we have to again write multiple statements so here
49:31
Speaker A
I can say a of 0 Z z0 position we want to say 100 okay and then a of same zero row zero row and First Column 200 so with this first row is completed zero row is completed now the second
49:53
Speaker A
a of 1 Z zero first col first row zero column here 300 then a of same row and column number one here four now the third thing a of two and here zero 500 a of two same row same Row 1 = to 600 so you
50:25
Speaker A
can have any number of rows and a number of columns this is how we can assign the data so this is approach one first declaration and then you have to specify number of rows and columns these are fixed and then we can add some data so again when we have to go with this approach if you
50:44
Speaker A
already know how much data you want to store in future you if you don't have any plan to add new data then you can go with this approach okay this is we can say f fix type of an array
50:57
Speaker A
this is one approach now in the second approach how can we add multiple rows and columns second approach now approach two see it is more simpler again first uh and one more thing is before doing this uh in the Declaration these brackets
51:30
Speaker A
you can keep in different places also same thing you can write like this okay you can write like this so we can put this brackets you can put this bracket remove from here you can put this before the variable also this is also right notation okay in some
51:50
Speaker A
places you can see this and by seeing this you should not be confused okay you can put those brackets before the variable also and you can put the bracket after the variable also or you can put one bracket before the variable another bracket after the after the variable this is also
52:07
Speaker A
correct all three notations are correct and this is also correct you can put two bracket before the variable is correct and the next one is what you can put bracket before and after and put this bracket one bracket before another bracket after so this is also correct notation okay so just
52:28
Speaker A
follow one approach all three syntaxes are correct you can put brackets both the brackets after the variable or you can put both the brackets before the variable or you can put bracket before one and after one anything is correct and same thing is applicable for single dimensional also if I
52:46
Speaker A
go back to the single dimensional so this is also correct and uh you can put like this okay you can put this bracket before the variable also this is also right notation and this is not there so you
53:02
Speaker A
can put the bracket before and after the variable so anything is correct this is another [Music] way I will put like this comment okay both are correct right now come back to the two Dimension so this is the first approach so we can declare an array and specify total number of rows and columns and
53:24
Speaker A
then we have added the data in all the positions now in the second approach how can we declare and store the data in two dimensional so let us comment this so now second approach first declare an array a two brackets this is same now curly Braes but here this time uh not only rows we
53:51
Speaker A
also have a columns right so how can we store it you have to follow different approach take one big curly braces now again inser this this is the first row one another cly base here you can specify the first row numbers in the first row that mean zero row what are the values we
54:11
Speaker A
have Z 100 and 200 so we can say 100 comma 200 so this is the first row that is your zero element zero row El comma open another C Bas here 300 comma 400 this is the second row elements comma
54:30
Speaker A
another places 500 comma 600 that's it so this is how we can store okay and if you're having confusion you can keep this in the next line like this you can it will be more clear right so first
54:45
Speaker A
row second row and third you can also put like this this is the way we can store data in two dimensional array so if we have another column no problem you can add another column like this here you can add another number okay so like this we can store data in two Dimension directly so
55:07
Speaker A
take one big curly braces inside this every row data we specify in the curly so this is the first row this is a second row third row and again put you can put comma and then you can add
55:18
Speaker A
more number of rows because this the last row so I'm just removing the comma so this is how we can store data in two dimensional array directly again this is more dynamic in future if you want to add more rows and columns you can still add okay and if you want to remove existing you
55:37
Speaker A
can directly remove it because it will allocate memory locations dynamically depends upon how much how many rows and how many columns you have added accordingly it will allocate memory okay so this is how we can store data in two dimensional array can go with the first approach or second
55:58
Speaker A
approach now we'll see the different operation how to find the size of an array size in the previous example uh we have only number of rows there are no columns so we have used length that will return return number of rows so here also same thing we will use to get the number of rows so to get
56:23
Speaker A
the number of rows in Array we can simply number of or we can say length of rows okay and here a DOT length so this will always gives the total number of rows even it is a single dimensional
56:42
Speaker A
or two dimensional a do length because in the two dimensional also we have a number of rows so when I say A do length obviously will WR three but how to find number of columns in a particular row where exactly the rows are columns are present in the particular Row first row is high priority
57:00
Speaker A
in the row contains the columns so if you want to find out number of columns in a specific row almost every row is having same equal number of columns so but we need to take at least one row
57:14
Speaker A
to find number of columns so how can we do this a to length this will give you length or number of rows but we want columns so what we can do here we specify the row number suppose when
57:31
Speaker A
you say zero here now this length keyword will find out number of columns in a zero row so for example let's try to P this okay so a of Z dot length and here I will write length of columns
57:57
Speaker A
and then concatenation just observe this so you can see here a of Z do length so this will give you number of columns in a particular row a of Z so when I run this this will give you total number
58:15
Speaker A
of rows are three columns are two right exactly same thing so almost every row is having same number of columns so you don't need to change the value but if we have a different number of columns in every row then specifically we have to specify from which row you want to find number of
58:33
Speaker A
columns so accordingly we have to specify the row number okay so if you want to find number of rows in a uh array we can directly say a do length array name do length if you want to find
58:46
Speaker A
number of columns first go to specific row and then find out length that will give you number of columns in a particular row okay okay so this is how we can get number of rows and number of columns in particular two dimensional array now the next operation how to read single value from
59:08
Speaker A
an array how to read a specific value or a single value from suppose I want to read a value from second row and First Column what is the value which we have here is 600 second row and First
59:24
Speaker A
Column so so how can we read it system mm a of two brackets second row First Column like this second row and First Column and I run this you can extract any value just by passing row number and column number so this will obviously six one specific value we're able to read and particular
59:51
Speaker A
row number and particular column you can just specify like this now if you want to read all the data I want to read all the data then how can we do this so these are the value you have
60:04
Speaker A
to specify in print statement 0 0 means 100 it will print 0 1 means 200 1 Z means 300 1 1 means 400 two Z means again 500 2 1 means 600 so multiple print tell and statements we have to
60:18
Speaker A
write okay multiple print statements we have to write what is the third row second column which one you're talking about here we have to pass index number remember so here 600 means what second row because index will start from zero okay index always start from zero that is a reason
60:41
Speaker A
this is second row not third row similarly column index also start from zero that is how we have a zero column First Column there is no second column in this you got my point VJ have understood so index will start from zero you should not forget that okay there is
61:05
Speaker A
no third row second column in this okay so do we have a third row in this array third row there is third no no so this is a index number when I say two two is an index number second row
61:21
Speaker A
where they say one one is an index number that is a column 600 so two and one means these are the index numbers second row and First Column second index is representing the row remember second row and First Column or second index and first index okay now we'll see how to read all the data from
61:43
Speaker A
two Dimension all the rows and all the columns everything we have to read so then how can we do this I want to read all the rows and all the columns then we have to write a looping statement so very important let's try to understand very clearly so if you want to
62:08
Speaker A
read all the rows and columns earlier we have only rows right so just one for Loop is enough to read rows we just incremented index value one by one and we have able we able to read the data
62:21
Speaker A
from single dimensional array now this time we also have columns in every row we have a columns so now what we have to do is we have to write two different for Loops one for Loop can have another for Loop that is nested for Loop we have to write two different for Loops okay one is for
62:45
Speaker A
incrementing the rows for one for Loop for every row we have to increment The Columns for that we have to write another for Loop so one for Loop should have another for Loop for each row we have to do multiple iterations row also we have to iterate parallely for every row columns also
63:08
Speaker A
iterate once all once we read all the columns from the first row then we will go to next row again once we read all the columns then we go to next row again read all the columns then we
63:17
Speaker A
go to next row so that's how we have to create two looping statements two Loops are required okay now let's see the classic F Loop first or normal F Loop by using index we can do it now so there are two for Loops are required so I understood right why two for Loops are required
63:42
Speaker A
because this time we have to increment number of rows at the same time we have to increment columns also that's the reason two for Loops are required so first one for Loop let say this is responsible for rows incrementation so where is the row number starts in I equal to0 and what is
64:03
Speaker A
the condition how many rows we have how many rows we have so we can say I less than or equal to 0 1 2 we can put two here I less than or equal to two or if you want to put number of rows you can say
64:22
Speaker A
I less than or less than three this is also right so for now I say I less than or equal to two then what is the condition every time I'll increment row number by one so now just observe here this
64:36
Speaker A
I is representing what i is representing what row number so instead of I let me take another variable so that you can remember R okay R I'm taking R is representing row number row index simply you can say row index so start from zero R less than or equal to two every time I'll
64:58
Speaker A
increment r value by one so now once you enter into this block so current value of R is what zero now in the zero row we have to read all the columns data so to read all the columns data again
65:14
Speaker A
we have to write one more for Loop inside this for Loop one more for Loop and here this inner for Loop is responsible for what incrementing the columns so here r value zero currently we are in the zero Row in the zero row we entered inside this block and this inner F should read
65:33
Speaker A
the data from all the columns from zero row so now where we have to start column number here column index start from what in Cal to Z column index also start from zero and how many times you have
65:48
Speaker A
to repeat this C less than or equal to how many times we have to repeat the columns zero and and one only two columns so you can say one two times you have to repeat and C value also incremented by
66:02
Speaker A
one every time we have to increment by one this is inner follow now come inside this observe here row number is what zero initially row number is what zero condition is true now it will come inside here initially c number is what zero c number is also what zero so zero row and here zero column
66:26
Speaker A
okay now come inside this now when you print this value a of R and here C we are passing row number we passing column number so r value zero that we are passing here and here C value zero that also
66:45
Speaker A
we are passing so what is the value we will get in the first iteration first iteration 0 0 means what 100 okay now it has got printed now after printing the statement C value will be incremented by one now what is the c value becomes now one so condition is true or false 1 less than or
67:08
Speaker A
equal to 1 condition is again true now again come inside now what is the current value of R still it is zero only and what is the current value of c one it becomes one so 0 and one 0 and one means
67:24
Speaker A
what 200 is got printed okay now C value will be incremented now it becomes two c value becomes two now condition two less than or equal to one true or false false so as soon as this condition is false then inner F Loop will exit and again it will go to outer F Loop and here r value will be
67:51
Speaker A
incremented by one so it becomes one here you got my point so once inner for Loop is got completed and executed finally it will come back to the outer for Loop and then incremented r value by one now r value one so it is focusing on first row again come to Inner for again it will restart from
68:12
Speaker A
zero so C value is what zero and one and Zer one and Zer means what here one and zero 300 so 300 is what yeah now C value will become one so one less than one again condition is true now r value one C
68:33
Speaker A
value is also one one and one which is 400 is got printed now C value becomes two then the condition become false so one soon as this condition become false then it will go to outer for loop again
68:52
Speaker A
here R value will be incremented by one again it becomes two now so again two less than or equal to two true or false true so again it will come inside again it is restarting for Loop so again
69:06
Speaker A
initial value zero it will start from zero C is zero now it will come here so current value of R is what two current value of C is what zero two and0 two and Z means what 500 you got printed now
69:23
Speaker A
see value becomes one 1 less than or equal to 1 true so again come here current value of R is what two current value of C is what one two and one two and one means what 600 is got printed now
69:42
Speaker A
C value becomes two then condition become false then it will go to outer for Loop and now r value becomes three as soon as R value becomes three 3 less than or equal to two condition fals then even
69:55
Speaker A
outer F Loop also will be exited okay so once the outer F Loop is got also exited then we will get all the values from array so this is how we have to write two different for Loops this is called
70:11
Speaker A
outer for Loop this is called inner for Loop once inner for Loop is reputations are completed then again it will go to outer for Loop value will be incremented then again come inside this Loop and again we'll repeat that means for every row we are repeating the number of columns suppose for
70:31
Speaker A
Loop is executed once and inner fop will execute two times again go to outer F Loop executed once again inner for will executed twice because for every row there are two columns okay so guys I understood how it is going to work so when you run this you will get all the values from array like
70:52
Speaker A
this all the values 100 200 3 4 5 6 everything we will get all the rows as well as all the columns okay again if you just want to print in the table format suppose I want to print 100 two line 200 in
71:08
Speaker A
one single line three and four and another line five and six and another line you can just format the output so just we need to change some pattern so instead of print Ln can simply say only print okay and so that these columns are there right one two this will print in one single
71:27
Speaker A
row after completion of the first row here you need to jump to the next line so just put only print Ln no value it is just for jumping into the next line because once you printed 100 and 200 in
71:41
Speaker A
single line then it will go to the next line then 300 400 will be printed and again go to the next line then 6 and 5 600 will be printed is just like a alignment see how now you can see 100
71:57
Speaker A
200 300 400 500 600 like this but again I want to give some space between this numbers are combined right so to give one space what you can do is here you can choose some concatenate with some space no value here just a space concatenate with some space after printing this value concatenate
72:19
Speaker A
with some space now when I run this exactly you will get a tabl format okay so this is how we can work with two dimensional areay so can we use length property for rows and columns yes so here
72:38
Speaker A
I have hardcoded the number of rows and columns so instead of that if you want to specify length if you don't know number of rows if you don't know number of columns and dynamically you want to get the data you can do it so you can say a DOT length a do length because we say less than or equal to
72:59
Speaker A
we should say minus one also okay because we say less than or equal to less than or equal to means sorry less than or less than or equal to means a do length minus one similarly here less than or equal to we are using so obviously it is observe this very simple now tell me this
73:18
Speaker A
one this is something new here it is fine a do length will give you length of number of rows and say minus one but here we want to get number of columns in this particular row so here we
73:30
Speaker A
want to get number of columns in this particular row right how to get it a of r dot length minus 1 okay you understood the statement second F why I have created because if you want to to find
73:54
Speaker A
number of columns in which row you want to find that you have to specify so a of R A of R means what array r r is representing row number R so in this particular zero row it will find number of
74:08
Speaker A
columns in the first in the second iteration of f Loop second iteration of inner F Loop R value becomes one so in the first row it will find total number of columns initially in the zero row it will find number of columns in the second round of iteration in the first row it will find
74:25
Speaker A
number of columns so dynamically you can put like this this will also gives the same thing y minus one here can anyone tell in the previous example I already told you Yus one is required Yus one is required okay let's go to previous example one more time
74:59
Speaker A
I think you guys are not listening properly some of you otherwise you won't get these questions maybe you you join late I don't know exactly see if I look at here when we have to use less than or equal to equal to or minus one and not minus one simply can say when I say A do length Okay a
75:20
Speaker A
do length that will return total number of Valu five five values are there totally five values are there but index will start from zero so guys I'm again repeating listen this carefully at least this time this is array okay why you have to put minus one I'm telling you 0 1 2 3 a four okay zero
75:43
Speaker A
index first index second third and four okay now you tell me how many number of values are there in this array let say 100 200 100 three four and five values I'm asking how many values we have values five values great how many indexes we have so index start from zero so starting index zero
76:13
Speaker A
what is the last index four four means what 5 - one right four means one 5 - one so index starting is zero the last one is four so if the number of values are five the last index should be what 5
76:30
Speaker A
minus one that is four so if the number of values are n the last index value should be n minus one is this clear first of all so number of values are n the last index value always should be n
76:44
Speaker A
minus one okay now based on this we have to put the condition now come back to the condition in the F Loop when I say I value starting from zero index is starting from zero when I say I less than
77:02
Speaker A
less than when I say less than what is the last index we have to specify four is the last Index right so how to get this last index we will get number of values length keyword will just return
77:14
Speaker A
number of values it will not return indexes okay so when I say A do length observe this when I say A do length is it correct or not a do length will return what number of values five it will return
77:29
Speaker A
what I'm saying here I less than five I less than five will it get all the values initially 0o 1 two also matching three also matching four is also matching but as soon as I value become five then
77:44
Speaker A
what happens condition Falls so then it will exit so when I say less than a do length is okay when I use use less than here a do length is okay but when I say a less than or equal to a less than or
78:00
Speaker A
equal to a do length is okay or not because a do length is five I less than or equal to 5 when you put I less than or equal to five there should be fifth location also right but there is no fifth
78:14
Speaker A
location here whenever I value becomes 4 four less than or equal to five still condition is true but as soon as I value become five then what happens five less than or equal to five condition is again true but here there is no fifth position right so that is the reason we say minus one then
78:34
Speaker A
it will become four right so when I use less than you have to just say a do length when I use l than equal to if you want to use less than or equal to then you can say a do length minus one you have to
78:50
Speaker A
use because last index you will get so total total number of values minus one is equal to last index so we have to repeat the loop till the last index number we should not go beyond that okay that is
79:06
Speaker A
the reason so because of that the same thing is applicable for two dimensional area also the two Dimension area also here we because we are using less than or equal to so that we say minus one here also okay this is thumb rule less than or equal to means you have to use if you want to
79:24
Speaker A
use only less than means just say only d. okay so this is how we can read data from two dimensional array okay so in the nexted for Loop is also same process every time here r value will be
79:44
Speaker A
incremented by one and again C value also will be incremented by one so once it comes here here it will substitute R and C C values r value 0 C value 0 0 0 then you'll get the first number the second of iteration inner for Loop will repeat okaye how it is going to work
80:06
Speaker A
just listen this this is outer for Loop this is inner F Loop inner F Loop repeated two times for one iteration of outer F Loop inner for Loop repeat two times after completion of inner for Loop then it will go to outer for Loop and incremented again it will come inside again in
80:25
Speaker A
will repeat two times then again it will go to it becomes three so for because in every row we have multiple columns also so we have to increment rows by using outer for parall for every row we have to increment columns also so once for first row we have incremented columns again second row
80:46
Speaker A
incremented columns third row we incremented columns like this it will work if you listen carefully there is no confusion actually so this is for outer for Loop this is for inner for Loop simple in outer for Loop representing the number of rows in Array inner for Loop is representing
81:08
Speaker A
number of columns in Array this is how the two dimensional array will store the data okay 0 1 2 0 one two zero Row first row second row zero column first column second column first we will focus on the first zero row then we will read all the columns so r value Z initially then
81:33
Speaker A
this inner F Loop will all read all the columns first 0er second one third two 0 one two like this once it is completed so first row is done now it will go to outer F Loop means second row
81:46
Speaker A
that means first row here and R value becomes one so again it will come inside and again this inner for Loop will repeat two times or three times depends upon the size and once it is completed then again it will go to outer F next row and again come inside this F Loop and this will
82:01
Speaker A
repeat again multiple times and inner for Loop is exited again go to outer F Loop then there are no more rows here then automatically it is exit so this is how nested F Loop works so guys everyone is clear please confirm in the chart window how exactly the nested for Loop
82:24
Speaker A
works now it will be more simpler if I use enhanced F okay let us try to use enhanced F it's more simpler than this one because we are not going to write any index is nothing it's very very simple when I use for each Loop or enhanced but just observe this okay so just first of all
82:58
Speaker A
I'm going to write this and then I'll explain how it exactly it is going to work very simple again here we need two for Loops okay because whenever you are dealing with the two dimensional array definitely we need two for Loops one for Loop internally is having another for Loop okay so
83:17
Speaker A
the outer for Loop is representing the number of rows inner for Loop is represent columns again so here because enhanced for Loop the a is array name colum in uh something uh let us say some ARR some array name and here ARR I'll tell you what exactly it is some X inex now here we just print x value
83:47
Speaker A
that's it no initialization this is simple this much of code is required so when I execute this you will get all the values from AR so get all the values so now let me show you how exactly it is going to work it is more easier than first one just observe this this is my two dimensional
84:12
Speaker A
array which contains currently we have three rows and two columns and we don't need to deal with any index again index will start from 01 to 01 no problem so 200 300 400 500 then 600 okay this is
84:30
Speaker A
my two Dimension now how it is going to read the data from array so this time a is representing the entire array okay so from a again we have a multiple rows 0 1 two so in the first round
84:46
Speaker A
of iteration what will happen is from a it will capture the entire Row the first first row how many values we have in the first row two values okay two values we have so in the first round of
85:02
Speaker A
iteration from a 100 and 200 will be captured and that I'm going to store in a variable and if you want to store multiple values into single variable what type of variable it should be it should be single dimensional array right so you want to hold multiple values into single variable
85:27
Speaker A
what type of variable it should be it should be single dimensional type of an array right so from a first it will capture 100 and 200 first row data that we are going to store in single Dimension array ARR is a single dimensional array now how many values it contains ARR contains how
85:46
Speaker A
many values what are they two values 100 and 200 okay remember 100 and 200 now now come inside and here ARR is what array which contains 10000 now in this again first 100 will store into X and here 100 will print second round 200 will assign into X now here 200 is printed after that there
86:10
Speaker A
are no more values in ARR then it will go to again outer F now second row 300400 will be stored into AR next round 3 and 400 now what are the current values of ARR 400 300 and 400 now come inside so
86:31
Speaker A
ARR contains 300 and 400 now first 300 will asend to the X and here x is printed as 300 next round 400 is assigned to the X then 400 is got printed next round there are no more values in ARR then
86:46
Speaker A
it will go to again outer F now in outer F Loop next time it will Le another row five 500 and 600 will assign to the AR 500 and 600 will assign to the ARR now come inside what is the first value
87:01
Speaker A
in a 500 500 will assign to the X then here 500 will printed next 600 will assign to the x 600 is got printed next no more values then it will go to outer for Loop in outer for Loop a doesn't
87:16
Speaker A
have any more rows all are completed so it will automatically exit so this is how enhanced for Loop works so now do you guys understood why we have taken single dimensional array variable in outer F why you have taken like this can we take a normal variable right we can take normal variable
87:41
Speaker A
but why we have taken array variable what is the reason behind that because a is a two-dimensional array in the two dimensional we have rows and columns the two Dimension array we have a rows and columns so first row second row third row again every row contains multiple values so
88:00
Speaker A
when I read a particular row we will get multiple values so to store this multiple values we need a single dimensional array normal variable is not enough to store those values similarly next row again we will get multiple values again same dimensional third row we will get again same as
88:20
Speaker A
soon as we get the values here we will again read individual values and we are Printing and again get another row next row next row okay so this is how enhanced for Loop works so when I execute it
88:36
Speaker A
this will print all the values 100 to 600 again if you want to print in the table format simply you can do only print print means it will print output only in single line and after completion of inner
88:50
Speaker A
for Loop just write one empty print L statement so that that will jump to the next line so when you run this this will print values like this in the TBL format again if you want to give some space in
89:05
Speaker A
X you can just put some space X is just a variable which will hold some data normal variable yeah now we got the TBL format so this is more simpler than the first approach so this is called enhance
89:23
Speaker A
load okay so is this clear [Music] everyone yeah so you can enable the debug point and you can check how we can exactly the statements are executing okay so let me just put some debug Point here so line number 59 I'm just
89:45
Speaker A
keeping toggle break point okay now let us try to execute this code in the debug mode so that we can see each and every L how it is going to execute so we have to choose debug as Java application
90:00
Speaker A
so whenever you wherever you put the debug point there it is stopped okay now just observe what is a contains see these are the values a contains 100 200 is the first row 3 400 is the second row
90:14
Speaker A
five and 600 is the next row okay now I'm just crossing the step to go to the next step what is the key we have to use F6 okay now I go to Next Step just observe ARR contains again see2
90:31
Speaker A
what does it mean we got a first row data you got my point so in a we have these many values three rows now the first row is what 100 and 200 once this step is crossed in the second step in ARR we
90:46
Speaker A
got only first row 10000 now when you cross this just observe X contains what 100 first value now in the second iteration second iteration 200 it is over now it will go to outer for loop again see this now a contains what all the values but when you cross the step just observe this time you got
91:13
Speaker A
a second row previously 10200 ARR value now it is 300 and 400 now I'm crossing this step first 300 is got printed next round 400 is got printed over then it will go to again outer follow okay same all the values are there now the last row 500 and 600 we should get cross the step now we
91:38
Speaker A
can see 500 and 600 so first 500 will be stored in x 500 next 600 will be stored in Excel done now it will again go to outer for Loop so all the values are there but all rows are completed
91:56
Speaker A
right so it will automatically exit so this is how we can debug the code step by step we can execute and see how the values are changing okay so just you practice this it will slowly uh you can be familiar with this and you can understand it's very very helpful to understand the flow of
92:18
Speaker A
execution because sometimes you may not be you may not if you execute the program directly and by seeing the out but internally how it is executing the flow if you want to check you can run the code in the debug mode just put one toggle Bak Point debug as Java application and
92:35
Speaker A
then shortcut is F6 when you press F6 function key it will execute step by step okay so this is all about two dimensional here now quickly I show you object type of an array it is more simpler so far
92:57
Speaker A
uh we understood one definition about Java what is the definition Java is a sorry array array is a collection of elements or collection of values of same data type that is official definition that means homogeneous data only we can store in Array only homogeneous data we can store mean same data
93:20
Speaker A
type values if you create create an integer array you have to store only integer if you create a double array you can store only double if you create array as a string you can store only strings but if you want to store heterogenous data that means if you want to store different type of
93:36
Speaker A
data in Array then how it is possible so that is also possible but that is not recommended actually but if you have a different type of data different data types of data you have available then you can directly go with the ER list ER list is one of the collection type so that is preferred most of
93:55
Speaker A
the times okay but we can also use array to store different type of data but this is unofficial not normally we don't use this but if in interview if anybody ask you can we store heterogeneous data in Array yes you can say yes by using object array we can do that so let me show you a quick
94:17
Speaker A
example how can we store different type of data uh in arrays it can be single or two Dimension anything is fine the concept is how we can store different type of data in arrays different data types how can we store okay which is normally called as object type of an array object array
94:41
Speaker A
fine so let me just create one array in a see in the first I'm storing integer second I'm storing decimal next I'm storing one character next I'm storing one string next I'm storing some Boolean see this this is called heterogeneous data heterogeneous data so if you want to store
95:08
Speaker A
heterogeneous data in Array it should not be specific data type if it is an integer it is not allowed so if you want to store heterogeneous data in an array variable that variable should be object type of variable actually what is an object this object is a predefined class in Java just
95:32
Speaker A
like a string in string s is a capital letter right so just like a string object is also one of the type or one of the class in Java is a root class actually all other classes are derived from
95:43
Speaker A
the same root object class in the coming sessions we will try to understand what exactly it is so this is a super class or this a root class of all predefined classes in Java for now just understand only this much in the coming sessions I will try to elaborate in the objectoriented
95:58
Speaker A
programming Concepts okay so if you want to store heterogeneous data the array type should be object so let me give you some example if you create int variable suppose int a I can store only number if you create double or float you create a double variable or float you can store only
96:22
Speaker A
decimal number if you create a car variable you can store only character like this if you create a string variable you can store only string if you create a Boolean variable if you create a Boolean variable you can store only true or false but if you create object type of variable okay
96:46
Speaker A
object type of variable this can hold any type of data okay remember this you can store integer you can store double you can store character you can store string you can store Boolean all kinds of data is allowed in object type of variable okay allowed all kinds of data X is allowed all kinds
97:11
Speaker A
of data just remember this point same thing for arrays also if you create an array as an integer I can store only integer values here if I create an array is a double I can store only decimal numbers in Array if you create a string array I can store only strings but if you create an
97:29
Speaker A
array with object type I can store different type of data okay now the next thing how to read this data let's write one enhanced for group simple right a from this a we want to read individual values into variable now what is the type of the variable should be be created here so every time
97:54
Speaker A
in every TR first time 100 you will get into the suppose here I'm creating one variable called X so first time 100 will store in X second time 10.5 third time a next time welcome next time true so different values are coming into X in every tration right so X is X should be able to hold
98:12
Speaker A
all kinds of data right so in that case what is the type of X what type you have to specify for x obviously this is also object right because in every iteration we are getting different type of data we are getting that's the reason X is an object so obviously you can just print you can
98:35
Speaker A
just print x value okay so this is how we can just read the data from single dimensional object type of an array right if you want to use normal for Loop by using using index you can still do that
98:54
Speaker A
suppose if you want to use normal for Loop for index will start from what Z index I equal to Z how many times we have to repeat how many times we have to repeat I less than a do length minus
99:08
Speaker A
one how many times we have to increase I ++ I representing the index okay I representing the index so here we can just print a of I that's it so this is a normal for Loop using normal for
99:25
Speaker A
Loop also we will able to read the data okay clear everyone is object type comes under nonprimitive yes that is also part of non-primitive because which is able to store all kinds of data okay so guys understood how we can work with the single Dimension array and the two dimensional
99:52
Speaker A
Ray this concept is not still completed actually I should give some more examples the tomorrow session we will see some more examples also we will discuss about string concept which is most important and once the strings concept is done we will also discuss some more examples from array
100:10
Speaker A
because this is the most important topic even in interviews also they will give you so many problem statements related to arrays concept especially there are so many things we can practice from arrays a lot of sorting searchings okay and uh printing the numbers in the reverse
100:26
Speaker A
order ascending order descending order sum of elements in Array finding even numbers in Array finding odd numbers in Array you can do lot of things the next two sessions one or two sessions we will spend time on this particular concept okay for today this is enough you guys
100:42
Speaker A
can practice these topics for now and uh I will give you two assignments based upon these topics first two topics whatever we discussed then you guys can try those assignments so these are the assignment first assignment find sum of elements in Array you guys can try this I will
101:02
Speaker A
also provide the video and if not understood tomorrow session we will discuss and tomorrow session we have something else to discuss but you guys can try this it's very simple first we need to read each individual value one by one and then we need to do sum of each and every
101:17
Speaker A
number let's say 1 + 2 + 3 + 4 + 5 equal to 50 so when you pass some data in Array we will get the total sum of all the elements so this we have to use looping statement for reading each and every
101:30
Speaker A
value as soon as you capture the value we have to add that value to the sum and take the sum value zero initially so I have created some video on this you can just go through the reference video
101:41
Speaker A
you will able to understand very simple very easy just reading the data from maray now second print even and odd numbers from array so this is also totally depends on the first one first we have to read this number and check it is even or odd number print it and then go to the next number and
101:58
Speaker A
check even or odd again go to the next number check even or not so for every digit or every value in the array you have to find even or odd number and you have to tell how many even numbers
102:09
Speaker A
are there how many odd numbers are there in this particular array okay so try these two assignments both are related to single dimensional array you guys can try this and uh the last somebody asked about prime number how to find a prime number so for that I have given one solution here you
102:28
Speaker A
guys can try this simple video I have created so what is a prime number and how to find the prime number through programmatically so what is the condition behind that so in this link you can just go through and in this video you'll able to understand what is just using by for Loop
102:44
Speaker A
you can simply do this actually so prime number to find the prime number you can just go to the video link and you can understand okay fine so this is all about assignments and tomorrow we'll still continue the topic strings along with arrays and we'll see some more examples and some
103:04
Speaker A
more assignments also I will give you tomorrow session and meanwhile you guys practice these assignments and if you stuck somewhere watch that video and I have clearly explained all most of the assignments I will provide the video links okay most of the assignments and very small
103:20
Speaker A
assignments easiest one you guys can try your own if video is not available other than that most of the them I provide the video links okay I already told you how to upload all your assignments yes can try that fine so I'll stop here for today's session and tomorrow's session we will continue
Topics:Java arrayssingle dimensional arraytwo dimensional arrayarray declarationarray indexingderived data typesSelenium automationJava programmingdata managementSDET QA

Frequently Asked Questions

What is an array in Java?

An array in Java is a collection of elements of the same data type stored in contiguous memory locations, allowing multiple values to be stored in a single variable.

How do you declare an array in Java?

You declare an array by specifying the data type followed by square brackets and the variable name, then use the 'new' keyword with the data type and size in brackets to allocate memory.

Why do array indices start at zero?

Array indices start at zero because the first element is stored at the initial memory location, and indexing from zero simplifies address calculation and memory management.

Get More with the Söz AI App

Transcribe recordings, audio files, and YouTube videos — with AI summaries, speaker detection, and unlimited transcriptions.

Or transcribe another YouTube video here →