Session 6- Working with Java Arrays | Single & two dimensional | Java & Selenium

SDET- QA16,137 words

Full Transcript

so in the previous class we have seen some 

Basics like uh how to work with the data types and variables Java variables and data types 

and uh 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 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 a time in one variable so one variable allow us 

to store only one single value at a time whereas uh derived data types we can store multiple 

values into one single variable so arrays is also one of the example of derived data type so 

if I create any variable with array type you can 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 type so that's the basic definition aray is 

a collection of uh 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 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 type okay and we can store multiple values into 

single variable so when it is required so when we have to use array concept one example let us say 

in a class there are 100 students are studying and I want to maintain 100 students of data let's 

say every student is having name every student is 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 specific variable is 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 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 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 same 

data type same data type means arrays allows 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 and suppose if it is an array variable so array variable means 

which can store multiple values right so how can we Define 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 uh 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 the bracket so this is a syntax 

the variable variable name single bracket equal to new inter 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 AR variable so that it will say five 

different locations okay five different locations but how we can 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 a 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 

can 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 should need 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 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 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 location so here also we can store the data so 

if number of values are five then obviously the 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 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 = 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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

Need a transcript for another video?

Get free YouTube transcripts with timestamps, translation, and download options.

Transcript content is sourced from YouTube's auto-generated captions or AI transcription. All video content belongs to the original creators. Terms of Service · DMCA Contact

Session 6- Working with Java Arrays | Single & two dimens...