hey everyone welcome back to another video and today we are starting with arrays so we'll start by what is array what are arrays and uh we'll look into arraylists we'll do some questions we'll do some really nice things today so let me just create a new structure basically a new folder i need to create a new project and next next in community classroom i can create lecture eight or something in the lecture lecture number eight erase so in this session if you don't know what our errors are we'll learn like into then we'll dive into it like from the basics why we need arrays what is the syntax of arrays and how this thing you know how we can code in it we'll also do some questions we'll also see how the memory management works for arrays in java and we'll be doing some nice things we'll see dynamic memory allocation we'll see you know we'll see arraylist we'll see how we can input from arrays how we can output from arrays and i'll also show you a little bit more about uh arraylists and all these other things let's get started finish that looks good all right so the first point that we will cover in this session is why we need arrays okay so let's say the question is store a roll number okay we have questions over here so we we say store a roll number how can you store a roll number so you will you can just say hey if you want to store a roll number you can just do something like int a is equal to 19. very simple another question is let's say store a person's name in some sort of a data type data structure you know so you can say okay if you want to store one name i can just store it in a name variable uh you can say nal gushwa okay so many things you can do but now the question is store five roll numbers store five roll numbers how are you going to do it int roll number one is equal to let's say 23 int roll number 2 is equal to 55 and roll number 3 is equal to 9 18 or whatever imagine the question was store 500 roll numbers would you write this line again and again would you declare and then initialize this again and again would would it not make sense if this there was a sort of a data structure where i could store this collection of data types we can use an array for this what is an array an array it's basically a collection of the your you know your data types it can either be primitives or your objects even complex data types whatever you want to store you can store it in an array okay so basically the syntax looks something like this syntax looks something like this so basically here you will have the data type okay brackets brackets basically means that this is an array so example here i am saying int roll number so in this case i will say int brackets brackets means it's saying that this reference variable is of an array similarly rno is a reference variable here i can say variable name is equal to new data type size let's look about it for a second okay let's look into it for a second look at this particular syntax over here which is the declaration syntax so basically if i say i want to store some roll numbers five roll numbers you know store five roll numbers i will explain in detail how this thing is working internally how the memory management is happening how this is mapping to the object or whatever what is this new keyword what is the size thing everything i'll explain what don't worry so i'll say int roll numbers are integer so i can say int i can say roll numbers is equal to new integer five roll numbers or you can also directly write this int roll numbers 2 is equal to directly you can write some rule numbers like 23 12 45 32 and 15. let's look into a few things point number one for people who are taking notes point number one this data type basically represents what is the type of data stored inside the array so array is what i already mentioned it's like a collection of data type you cannot do something like this you cannot do something like okay first two number first two elements of my array will be integers second num second element will be a string you can't do that so this is point number two for people who are taking notes point number two all the type of the data in the array should be same you cannot add you cannot mix and match data types like okay first element will be integer second will be float third will be string then again integer then again boolean or whatever no if you are making an integer array it will contain integers only if you are making boolean array it will contain boolean only if you're making an array of string it will contain string only okay if you're making an array of some complex data type like your own data type which we will do in object 20 programming in that case it will be update data type only and where does that data phi data type is defined where is it defined it's defined over here so this int bracket basically means rnos this reference variable is pointing to an array object that contains type integer elements okay so this thing is explained this thing is explained how this is pointing to the memory and everything i'll explain in like a little bit so why we need arrays uh that thing is done and what are areas that thing is done syntax is done okay so one more thing i want to share in syntax is that uh you know uh how this thing is actually working so basically um this reference variable it's going to be pointing to this array object so this right hand side thing that you have over here it's the object and this is the reference variable let's dive into it let's open this up and look at every single thing in detail so the thing that is happening over here is something something like this if i break down this step this is known as declaration of array okay so here what is happening here what is happening here ros is getting defined in the stack okay it's just getting defined like hey this ros reference variable currently it's not pointing to anything so we need to point it to an object actual memory creation happens here when i say ros is equal to new integer this is actual memory creation actually here object is being created in the memory heap memory we already mentioned in the few sessions if you want to see the complete dsa playlist check out the links in the description so basically this is just the declaration and this is the initialization basically initialization here the actual object is being created in heap okay so let's look and look into it in a little bit i'll create an entire diagram and explain it to you so let's see how areas work let's see what are these indices how we access these indices what is this heap and stack i'm talking about let's create a nice diagram and explain this but this is the syntax to create an array you have to mention the size over here or you can directly just write the arrays like this if you don't mention the size it will give you error okay let's look into how these things are working internally so when i wrote something like you know int roll numbers or whatever i can just say in a r also let's say arr whatever is equal to new integer size colon this is what data type this is what reference variable this thing is what creating the object in heap memory creating the object in keep memory okay so that is how thing this thing is working let's talk a little bit more about like how it's working internally and how these things are actually being you know allocated how the memory management you know works in java so i told you previously the the things that are on the left hand side of equal to that is like at runtime that's like a shortcut to remember this so this initialization and everything happens at right this declaration and everything like happens at runtime actual memory object like the actual object that is being created in the memory in the heap memory and if you want to learn about heap memory stack memory we have a separate video on that already that happens at run time so this happened sorry this this thing happens at compile time and this thing happens at run time compile time the actual object creating in the heap happens at runtime so this is basically known as dynamic memory allocation what is dynamic memory allocation at the runtime memory is allocated that is dynamic memory allocation okay this thing happens at compile time this thing happens at runtime internally it works something like this so yeah i already talked about like dynamic memory allocation so assigning the memory space during the execution time or like the runtime internally how is it thing how how does it work so basically you have the stack memory in which all the function calls and reference variables go a r will go over here and the actual array object something like this 3 18 9 2 this will be pointing to this object okay so this is the reference variable and this is the object basically okay yep it's recording cool so whenever you want to get an item from the array so when you say i want to get the first item one thing you have to notice is that the array indices if you talk about the indices if this is like the stack memory there's a heap memory okay we talked about this in detail heap memory stack memory you know jvm manages and all these things i hope people who are regularly following the course they are able to understand very simple stuff we went into it in detail when we were doing the java architecture okay so array representation internally you can imagine something like this you know it's like uh somewhat like this okay a box of uh you know collection and let's say we are taking an integer array for example can i contain anything numbers random numbers okay so in many other languages these blocks of memory so this is a simple memory block another memory block another memory block in your ram or whatever this is stored continuously in cc plus okay so array in cc plus plus it's like a continuous memory allocation what do you mean by continuous memory because in your ram you may have some cells like this okay in your ram you may have some cells like this each cell is going to have an address okay each cell you're going to have an address and in the array basically all the cells of the array are continuous memory allocations it's not like first three elements will be here next three will be over somewhere over here skipping these two no this is how it works in cc plus plus java java says something different in java there is no concept of pointers you cannot get the address or anything so it totally depends on the jvm you know whether this is going to be continuous or not okay why because of this reason we know point number one we know that array objects are in heap we know this because we know that objects are created in heap not just array objects all the other objects you know also that we'll be making of like let's say object-oriented programming and stuff we know that those will be stored in the heap section so that is something we know and uh the second thing we know is that the if you check out the jls which is the java language specification where they have mentioned about the internals and workings of java in that they have specifically mentioned that heap objects are not continuous okay there is no like confident proof of this like it may continuous maybe not continuous whatever they have mentioned that heap objects are not continuous you know what continuous means now okay it means that the cells memory are like one by one allocated not like something is missing in between for the object okay point number two point number two we know that heap objects are not continuous i believe you know what continuous is now continuous basically means if this is your ram something like this continuous basically means if this array is allocated in cc plus plus it means that it will be allocated for continuous blocks like this it will not be like index number one lies here index another index lies here another index lies somewhere over there no truly continuous block of memory but heap objects are not continuous java says that okay and the heap is the runtime data area from which the memory for all the classes and instances of like all the arrays is allocated we also talked about this right now dynamic memory allocation when you program after compiling so basically the declaration happens like at compile time and when it's like uh actually creating the memory actually assigning some memory actually creating that array in the you know ram or whatever object memory that happens at runtime dynamic memory allocation okay point number three point number three dynamic memory allocation okay so heap area is not continuous and we know that array objects are also in the heap area hence array objects in java may not be continuous okay even though the definition of array says it's a continuous data this is the definition of array if you google it but in java internally may not be continuous if someone asks you what might be the reason this is the reason and you can just answer them that it depends on jvm why does it depend on jvm for people who are following the course you will already know heap memory stack memory all these things are of no jvm only that is the reason it depends on jvm all right cool this thing is covered and i also covered that uh one more thing i want to share is like the indices of arrays so for example if you have an array like this and you want to get the first item so basically there is something called index of an array what do we mean by index index are just numbers that basically means it's like the um position of the array which starts from zero so it's going to start from zero next one is going to be one then two then three then four then five and six if there's a reference variable arr that is pointing to this array and you try to print you try to print something like arr of 0. this basically means what element is at the zeroth index of this array it's going to print you three when you print something like arr of two so it's going to print whatever is at the second index which is 19. similar things go goes for updation okay so arrays are basically i'll talk more about this the mutability behavior i'll talk shortly but uh if you want to change something you say arr of 3 is equal to 99 so basically this will be changed to 99. okay so we talked about memory management we talked about being continuous or not we talked about jvm stuff we talked about the syntax in java we talked about what are arrays we talked about why areas we talked about you know the declaration so only the reference variable is assigned and in the initialization using the new keyword yeah new keyword keyword keyword is remaining let's talk a little bit more about new keyword what is this new keyword what is this new keyword nu basically just means it is used to create an object okay new is used to create an object okay new is used to create an object so here if i'm saying new int dash like brackets 5 it will create an object in the heap memory of array size 5 that is what new is okay so this thing happens like at dynamic memory allocation at runtime that is why this new thing is here or you can do something like this if you are not using new if you're directly assigning it something like you know like we did previously 2 18 19 12 or whatever even though you're not putting new over here this is also uh the java internal syntax uh internally it's doing the same thing okay internally it's doing the same thing so new is a keyword that is used to create an object okay here it's saying create an array object of size five it will create an array object of empty size five initially when we don't put anything inside the array we will see what is available over there let's try it out okay so we talked about indices so for example here if i have something like you know let's say if there's initially just five uh elements and nothing is over there if i print ros of what is at the let's say first index because it's a size five let's try to print this it's gonna show zero i believe zero okay so all the integer values or for for an integer array basically by default it's just going to have all the zero elements okay everything is going to be zero in it basically it's going to be internally like this if it's five size then zero zero zero zero zero internally okay let's try something else let's try making a string array something that takes a string if i say string array arr i can say new string size 4 maybe let's try to print what is inside this you know string array by default null okay so this is a separate thing in java null it basically does not like like represent like a database or whatever uh null basically means that uh it does not you know really have any data type for example it's null for example you can compare it with none in c plus sorry you can compare it with none in uh python python has none here you have none basically okay so people who might be having some questions about what is null quantile you have never seen null before it's not like a keyword or anything it's like a special literal okay so we talked about literals before um and um it's a it's a special literal of a null type so basically you can assign it to any particular like you can cast it to any particular reference type but you can't create a null type okay you can't like really uh you know declare this variable type you can't say something like this you can't say null a is equal to something no you can't you can't create it something like this if i try no null or whatever no can't do it it's like a literal okay it's a literal if it's saying power power sent or whatever you can see it's saying um whatever getting giving us errors basically so it's just like a special type it's like uh you know you can't you reference it for like in primitives for example uh some example you can reference it to something like uh string string a is equal to null you can do that a is already defined string str is equal to null but in num this will give error you cannot assign it to any um you cannot assign it to any primitives basically okay you can only assign it to non-primitives it's just like a special literal that is like you know null value and compare it to with none in python all right even if you're having more conceptions about kunal what is null or whatever then basically just it's you know in simple languages i can just share that any reference variable that you have by default it's going to have the null type for example this is a reference variable ros and it is not at this line it's not assigned to anything so you'll be like hey kunal what is the value of this thing well the value of this is by default going to be null okay similarly in integer arrays by the way we will talk about how this thing works internally basically here there are four objects so we'll talk more about this internally and why this is showing null i'll explain it to you in a little bit why this thing is showing null why why every particular element of this is null we will share about that in a minute but just understand that it's not a data type or anything it's just you know it's not an object it's not a type it's just a special value which can be assigned to any reference type and you can type cast null to any type as well but you can't do it for primitives okay it's a special value which is the by default value of reference variables as simple as it gets special value literal not an object or not a data type or whatever special value that is the by default value of what reference variables point to let's dive into it let's dive into null and let's see why this uh you know by default everything is null over here so internally what happens in java is like okay you have primitives and whatever that's fine but what if you have an array of objects and not primitives for example if i have a string array arr is equal to new string let's say five elements okay internally what happens is internal working off object arrays not primitives here you can see this is an object type means uh it's not a primitive type string will have an object in the heap memory by the way we already talked about this that primitives are actually you know they are stored in the stack memory only like in car boot or whatever they are stored in the stack memory primitives and uh for example in the first lecture when we did like you know stack memory heap memory concept that was specifically i mentioned it was about objects and references okay for example in python there are no primitives everything is an object so here in java note this primitives are stored in the stack memory only but all the other objects that like string type array type your own type all these other things you know hash map or whatever that will be making all these objects all the classes that we'll be making they are stored in the heap memory okay we'll do an example of that as well internal working looks something like this so you have your stack memory arr is available over here so this is your stack memory stack memory and arr is here pointing to the heap memory that contains the array string it contains five elements now each particular element itself is an object and this entire each object will be stored in different parts of the memory okay that's how it works internally so each particular thing is an object and we know that this is going to be another reference variable some reference variable here for this might be something like arr of 0 and since this is just initialized it is not you know we have not said arr of 0 is equal to let's say it's a kunal or 3 or whatever in string so by default we know all the reference variables point to none null so that is why it's giving us null to sum up each particular object that we have over here it's going to be in separate areas in the heap memory these are just like reference variables over here that are pointing to those objects that can be any anywhere in the memory and then we have like the so these reference variables they may be like closely linked together whatever when you're talking about like continuous memory allocation or whatever but that is like not a proof in java like okay it's not like java definitely has continuous memory allocation this is actually an array of reference variables when you're doing object okay understand the difference between primitives and objects primitives are int care boolean and your float and whatever and non-primitives are your complex data types what are primitives you already talked about it something that cannot be broken down string can be broken down into characters array can be broken down to individual elements bigger complex data types can also be broken down as we'll see in object-oriented programming so objects are stored in heap memory stack may in stacks there are stored the reference variable will point to the heap memory point number one we have discussed that multiple times second point if you know each particular object if each particular element of the uh array is an object that it will be like located in some other memory and the reference variable will be pointing to it's like this array is actually just a collection of reference variables okay and by default for every reference variable if there is no value it's none none all right so let's look into it like for example let's say we take uh something like this so we have this array or let's take the integer one only let me create a new one let me create a new one this one looks a little dirty so i can say something like uh input so we'll be like doing the input thing over here public static void main okay here you can see this is also a string array so now you know args is actually a string array okay so let's do something related to input samsung integer array arr is equal to new integer of size 5 okay so it's starting from 0 if the index is starting from 0 we know that the last index value is going to be size minus 1 which is 5 minus 1 which is 4 so i can say a r of 0 is equal to 23 arr of 1 is equal to 45 and so on and so forth so basically if i just print a rf i can say arr of no it's very time consuming but anyway two three and four let's say 40 something like this something like this and something like this if i print the array index number three it's basically going to print me what is that index number three 543 it's printing me 543 internally it's now stored something like this you know something like this 23 45 233 543 and 3 okay very simple stuff but if the array was like 1000 size and i i wanted to fill this 1000 times this will be very repetitive can't we do it like easily we can we can use a for loop so i can see something like uh input using for loops so basically i'm going to say for every index i starting from 0 going till where going to till the length of the array here i know that it's 5 i can put less than 5 over here because then it will go for index number 0 1 2 3 4 that's what i want to do but many times you might not know the size of the array so for example you can do a r dot length so you can see length is an integer variable that is public for you know in the array class we'll learn more about this in object entry programming you can see it's a length variable which represents the length of the array so here i can just say arr of i is going to be equal to input we can take an input we can say scanner n is equal to new scanner system dot in so here i can say in dot next int so whenever you take a next integer it's going to be stored in the ar out of i initially the value will be zero then it will print okay ar 0 is equal to take the input then the value will be 1 a error of 1 is equal to take the input then so on and so forth okay that is pretty much about it one more thing i want to share is that you can also print this in the same way so you can run the same for loop this time instead of taking an input you can just say print arr of i print arr of i i can maybe print it in a single line let's try to run this i know it's asking for five elements because around length is five so it's going to ask for five elements like let's say 23 3 4 6 57 in the next line it is going to run this for loop to print it so it ran this for loop took the input then it ran this for loop and every time it just printed it out that's it this is showing showing some highlight it is called a for each loop so basically another point for you all you can definitely just iterate over like go through these indices because here you are just using the index to get the item you can directly get the item as well using an enhanced for loop replaced with enhanced for let's try that out okay so basically this is the syntax for that for data type reference variable in array print that type so basically here i'll write it as a comment as well here num represents element of the array so element number one element number two element number three four for every element in the array print the element for every element in array print the element that is what we're doing here if i hide this is known as for each loop okay if i type five numbers here okay obviously if you type if you try to get the answer to more than five numbers that will give you an array an error so basically if you're saying print arr off let's say the last index is what zero one two three four so if you try to print a r of five this will give you error it's saying index out of bound you know index out of bound the last index is index number four because the size is specified as five how are you printing size index five index five does not exist error so index out of bound error same thing if you write some negatives or whatever okay cool one more very nice way in order to print so i showed you two ways to print one easy way to print is actually do something like this print there's an arrays class dot to string and then you just pass the array over here array class has a two string method when you give an array into it it basically converts it into a string and prints that string so you can see it's taking an integer array over here and you know if the length is uh you know if the length is like if the integer is empty it is returning empty empty braces if it's null that's returning null otherwise it's adding it's using string builder string builder is something we'll talk more about in the later sections and here just adding comma and everything formatting it for us and using a simple for loop same thing that we were doing okay if i try to run this it will just without any for loop internally it's using for loop only you can see internally it's using for loop only nothing new over here okay i just write five numbers here like one two three four five it's printing it's adding the brackets adding comma and everything for us that is true adding the brackets adding the comma adding the numbers and whatever okay three ways to print and this is the best one okay this is a very nice alrighty cool now if you want to learn more about let's say we talk more about let's say we take a string array okay we take a string array you know we take an array of objects this was like array of primitives okay this is an array of primitives now we do array of objects we already did this let's say string str is equal to something like new string name sorry not name what am i doing let's say for size okay and then i can say for i 0 i less than str dot length i plus plus i can just say is equal to in dot next that's it then i can just print it arrays dot to string arr sorry not a r str okay let me just hide this thing initially you know initially you know when i do this thing here the declaration has happened here the initialization means the dynamic memory allocation actually the thing has been created in the heap and by default it's going to contain four objects in it because the size is four and by default it's basically actually containing the reference variables to the objects and those objects those objects can be stored anywhere in the heap i'll draw a diagram of this soon one second when i run this let's say i just input four strings like abcd a b c and d it's going to print a b c d don't think that this is a character this is a string okay this can be a string as well if you want to do a bb cc dd internally how it's being stored let's look into it so here you can see on the diagram that is what is happening internally also basically you have your you know you have your stack memory and you have your heap memory so in the stack memory you have something like str in the stack memory which is like a pointing in the heap memory heap memory and actually contains a array of size four it's going to have four reference variables okay some reference variables over here there's some object a over here some object bb over here some object cc over here some object dd over here this is pointing to this this is pointing to this this is pointing to this one this is pointing to this one it's storing reference variables to these objects these objects can be anywhere in the heap i hope that it is clear and it is very simple stuff nothing nothing new over here and we have already done this many many many many types already done object heap memory and all these things we have done so many times okay cool now let's talk about multi-dimensional arrays like 2d arrays how 2d arrays work how 2d arrays are stored and stuff so we already talked about the input we already talked about the output in three ways and you can definitely modify it as well so if you say even though it's a b b c d so you can say modify so i can say let's say the first index should be something like kunal then i print this again so it will be changed to kunal directly accessing the element and changing it okay so you can definitely do that as well this is actually directly getting the references saying this is the reference variable that thing this thing this a this thing where that we have over here okay this uh a this first star that you see this is actually str of 0 this is strf1 next one is xtr of 2 last one is str of 3 so basically you're getting that reference variable directly over here and changing its value hence the original one will be changed very simple stuff we have done something before also okay if you initially just write a a b b c c and d d afterwards you can see string one is changed to kunal's original string object is changed that is true you're changing the original object that is what we're doing so you're doing something like this you're saying change this to kunal that's what you're doing okay cool let me see if i missed anything or not so i believe we covered every like the basic stuff over here and with 2d arrays i'll explain it to you shortly i already mentioned and stressed on a lot of points multiple times we talked about how this is working internally we talked about how you can input it like this and how you can input it by a for loop we talked about memory management we talked about heap and we talked about primitive array we talked about the other type array like the or area of objects and how that is stored internally we talked about arrays or two string and how you can modify one more thing i want to share is like we you know function example now one more thing you know we have we are talking about references objects and all these things for so many lectures now let's try to see how these things behave in functions in the previous lecture we did about you know we learned about functions and we also learned how data types and like reference variables are passed into functions in java there's only call by value and when we pass an object's reference it's actually going to take a copy of that reference which is going to point to the same object let's try that we already did this in the functions section so we already ran this example also in the function section so nothing new over here passing in functions let's make something like this and then i can just say public static void main i can create a function over here static void change it's taking an integer array of let's say arr or whatever and saying the 0th index change it to 99 let's say i create an original index array over here integer array it's called nums that takes something like 3 4 5 and 12 or whatever i'm saying print this so i'm saying print arrays dot tostring nums obviously it's going to print 3 4 5 12. but after that i'm saying change the value of the 0th index via this method so just pass this nums array in the method what is going to happen over here let's look at our diagram let us look at the diagram what is going to happen over here we have done this many many times you might be aware of it already now so basically initially we have something like this so we have nums reference variable pointing to an object of array like this which is 3 4 5 and 12. then the copy of this reference variable is passed in the function let's say the name of the reference variable in the function what is the name of the reference variable in the function arr but it's pointing to the same object and what am i what am i doing in the function in the function i'm doing a r of 0 is equal to 99 means this object's 0th index should be changed to 99 isn't now the original value being changed the original one is also changed right because both of these are pointing to same object this is known as mutability mutable behavior okay so strings are immutable in java and arrays are mutable in java mutable means in simple language you can change the object you can change the object why are strings strings immutable why cannot we not change strings we will look that into the string module but if i run this it will actually change the original array as well ta-da okay now let's talk about 2d arrays in java how do 2d arrays work basically 2drx you can imagine it as a matrix so for example i just say dimension array or whatever public static void mean so you want it to look something like this let's say you want it to look something like this or why am i writing the syntax i can just directly show you so for 2d arrays basically if you're making a two-dimensional array it should be like a matrix sort of a thing for example i can just write it directly it can if you want to print something like this if you want to say create an array that is a matrix like this one two three four five six seven eight or it's like too big i can say one two three four five six 7 8 9 so in this case you are going to have three rows three columns how this works internally and everything will look later on but let's first look into how we are defining this so 1d array is defined like this with one bracket 2d array is defined with two brackets okay and i can say arr is equal to new integer i can pass a size over here let's say the number of rows that we are going to have is 3 number of columns is not necessary to specify why we will look into it shortly when we discuss how this is actually being stored in the memory then we will discuss about it adding the rows is mandatory it's not like you are adding the columns only no this is not mandatory the number of rows is mandatory is the syntax here there you go number of rows here you add number of columns you can definitely add number of columns as well but it's not mandatory okay cool and if you want to store it like this like you know in the hard like the hard way we were doing it like directly if you want to store something so you can do you can do something like this sorry not this bracket but like curly braces you can store something like comma comma comma and then this one two three four five six seven eight nine things will be much more clear when we see how this thing is actually being stored in the memory without further ado before we try input output before we try you know the size and everything whatever those things let's see how this thing is working internally let's see so in cc plus plus this is also continuous memory allocation okay even in 2d mary arrays it's continuous memory allocation in java it's something different so in java it's saying if you have an array something like this this is uh you know it's pointing to an object and internally it stores something like this one two three four five six seven eight nine let's understand how this is being stored stack memory heap memory understand how this is being stored so basically how is this being stored every single you know every single thing that you have over here in terms of in terms of the uh you know arrays so basically you can imagine this as an array of arrays meaning the zeroth index is an array itself the first index is an array itself the second index is an error itself array of arrays you can imagine it's something like this okay that is how it's like actually stored in the memory in the memory how it's stored in the heap section basically it stores something like this so if you have array like this stored in the heap memory like this this is the main array that is the outside array and internally it has three internal arrays this can be in some other part of the heap this will be in some other part of the heap this will be some other part of the heap same thing like we discussed previously individual ones are basically you know just like the references to this individual arrays so this will be individual array like 1 2 3 this will be individual array four five six this will be individual array seven eight nine these are like going to be references to these arrays that's how it's stored internally even if you want to do 3d arrays that will add just one more dimension to it you can make 3d arrays as well you cannot visualize after 3d arrays if you're working with higher dimensions than that then use python and numpy and stuff but you can visualize to 3d arrays you can do 3d arrays in java as well you can do more than that as well but it's hard to visualize after three dimensions so basically each particular index is itself an array and that is in some other memory location in the heap so when you take arr of let's say 1 it's going to give you what this will give you 4 5 6 and when you do the index of this itself so when you do a r r of 1 comma 0 so it's going to say okay arrow one is what the first index of this 2d array first index of this 2d array contains an in an array itself and then it's asking what is the 0th index of that 4 this will give you 4. okay i hope this thing is clear and it's very simple stuff next thing i want to mention that since these are individual now you know i mentioned i will explain why column size is not necessary column size is not necessary because how many number of arrays do you want to have that thing should be there because that that way the you know heap memory and everything will be created but for individual columns this column has three this this row has three columns this is row one this is row two this is row three this has three columns this has three column this has three column it can definitely have variable number of columns the size of the individual rows does not matter it can also be something like this your array can be something like this one two three four five six seven eight nine it can be something like this as well let's try it out let's try it out let us try this out so i can say something like one two three four five six seven eight nine isn't it good individual size of error can vary as well because each array itself is a different object that is why it is not necessary it will not give me error if i don't add a this thing over here the number of columns so number of rows is three number of column in the first one is three number of column in the second one is two number of columns in the third third array is one two three four four okay that is how it's stored inside the memory so individuals can ones can have the different size now let's look at the very basic stuff which is known as input and output so i can say first let's look into input how are we going to take the input for input i'm going to say that um zeroth index first index second index okay so when i say for example arr 2d of 2 this will give me 6 7 8 9. when i do a r r of 2 d of 2 of 0 this will give me 6. okay so how do we input we will run a for loop for every index we will say ok first fill this particular array then fill this particular array then fill this particular array we are doing the same thing that we were doing over here input this thing we are saying take this entire input this is an input for one array we need to do the same thing for individual arrays we need to do this multiple times let me take the scanner okay so we need to do this thing arr i can just hide it over here i can make this arr for example okay so this is something we need to do so basically we need to do this thing for every index of my original array at zeroth index it's going to have hey at zeroth index i have this particular array please fill this out initially let's say we have three comma three so initially let's say we have three comma three so i have something like uh int arr is equal to new int 3 comma 3 so here i am going to say for every row so i can say basically for every row what is the length of the row by the way this is another important point the length of the array when you do when you print the length of the array array.length this will actually give me number of rows so even if this was something like 3 comma 4 this will print me 3 okay cool so here i can just say 3 comma 2 for now so here i can say arr dot length number of rows and for every row for each column in every row i want to take the input again another for loop this time for every column i will explain this with the diagram as well don't worry column zero this time column will be till where this will be column this will be having let's say size of three this will be having size of two this will having size of four even though in in this case every column has a size of two but the size of the column is varying for every every particular index of the array let's say the array at zeroth index have a size of three area of the first index have a size of two so here we are going to say every array at that index every array at each at that row the length of that array every array at that row the length of that array that is what we want okay we will run this we will run this don't worry i will completely run this and then i can say arr of row and column is equal to in dot next end okay let's try to run this let's try to run this let's try to see how this is working internally let us try to see how this thing is working internally so we have two things over here i am saying that initially it's a empty array of three comma two so basically three comma two means we have empty array like this index 0 1 2 0 and 1 these are my rows these are my columns okay so what am i doing in the for loop in the for loop i am saying that for rho is equal to 0 row should be less than the you know length which is equal to 3 and row plus plus then for every column in that row now we are taking every row here we take every row and we know that every row itself is an array of size 2. you know every row itself is an array of size 2. so we will iterate on this array now so we will say for every column starting from 0 column should be less than here less than 2 column plus plus take the input at array of row comma column is equal to input that's it program finish let's try to dry run this so initially my row value is going to be 0 then it's going to say okay here row value of 0 for for row value 0 try to run this entire for loop it will run this entire for loop for row is equal to 0. so rho is equal to 0 right now row and column let's say row is equal to 0 and now column is equal to 0. so it's going to say okay column is equal to 0 is column 0 less than 2 it's going to be say yes it is then just it's going to say add array of row comma column means array of 0 comma 0 take the input for that i add something 23 over here then it will come out of this for loop it will in increment the column by one then it's gonna be like is one less than two it's gonna be like yes it is okay then zero comma one should be another input zero comma one another input let's say 18 value will be two is two less than two no it's not okay come out of this for loop when i come out of this for loop this loop is now run once so for 0 it will now be coming out of the for loop this outside for loop will run again row plus plus this will be 1 now when i do this one now it's going to take for this particular row call will be again be zero because this group will and this loop will run again from the start we already talked about the scoping and everything in previous lectures so again call will initialize to zero then it's going to say is zero less than 2 it's going to say yes it is ok then row comma column means 1 comma 0 take the input 1 comma 0 take the input as 9 or whatever and then increment the value call plus 1 w 1 then ok is 1 less than two yes it is so one comma one is again again input let's say 27 value is equal to two and then it's going to say hey is a two less than two it's going to be like no it's not okay then come out as for loop this for loop for one is over row plus plus rho will now become 2 then it's going to check is 2 less than 3 yes it is okay then go inside the for loop again the for loop will be initialized again call is equal to 0 will be initialized okay let me zoom in like a little bit so that you can see okay again it will be initialized so it's going to say okay call of 2 comma 0 is input to 2 comma 0 it's input let's say 37 33 or 37 whatever that's gonna be like okay plus one is one less than two yes it is call of two comma one can be something two comma one again input two comma one again input let's say eight then it's going to be 2 it's going to be like hey it's 2 less than 2 i'm like no it's not then it's going to move out of this value 3 is 3 less than 3 no it's not program over array is filled so in simple language outer for loop it's iterating each row for every row i am iterating every column using another for loop so we already understood how to take input in a one-dimensional array in a 1d array i am doing that same thing for every row okay so here i was basically saying this 2 instead of this i was just saying array of row dot length imagine it was something like this okay let me just uh yeah array of row.length so imagine if it was something like this if my array was something like this let's say this is the outside like array first one contains let's say three elements second one contains only two elements third one contains like four elements so here for the outside for loop it will be like for row is equal to 0 row is less than 3 row plus plus now for every column what is it going to do column is equal to 0 column is less than what 3 2 or 4. size of the row size of row column plus plus input that's it for example when you run row for the very first time v 0 column will be 0 column is going to be less than size of the row what is size of array of 0 3 hence this will go till 3 0 1 2 when it will run again for i is equal to 1 what is the size of array of 1 actually equal to 2 so it will actually go till less than 2 means 0 and 1. okay very simple stuff that is the reason we are using this thing this is the reason is actually saying individual size of the row only we need that many amount of columns obviously if first row has three columns take input of three columns if second row has two columns run the for loop for the columns twice only if third row has four columns run the for loop four times for input okay cool so that is what we are doing over here and uh let me now also you know show this thing in action so basically how do we print this do the copy instead of taking an input now we can take the output so instead of taking the input just say print this thing print this thing after every column is being printed after every row is printed so this is iterating over every row for first row it's going to say okay print the first line for second row playing the second line so my question to you is when the first line has been printed let's try to run this first okay let's try to run this first so i say i can input something like one two three four five five six seven eight nine basically it was taking just three comma two so it's taking six only so that's why it's printing six but let's say i make an array of 3 comma 3 so like the 3 comma 3 is 9 elements so i can just input 9 line events over here it will definitely print all the 9 elements 1 2 3 4 5 6 7 8 9. why is it not printing in a matrix form because you did not add a new line over here after printing every single row should you not print a new line print row number one then print new line row number two print another line now it will look much better let's see one two three four five six seven eight nine now it's looking like a matrix print row number one when it comes out of this for loop when it prints the entire like you know this row it comes to the new line new line new line okay we also saw that uh you can use arrays.tostring so can't you do something like this another way to print this can be first of all enhanced for loop you know here basically enhanced for loop it's very simple we will talk more about that later but here what you can do is actually we can replace this with arrays.tostring what do we need to pass every individual array array of row that's it because this is actually printing just this entire internal for loop is same as this thing right same as this thing so if i'm able to replace a single thing with a you know arrange.2 string why can't i do this with the other one let's run this four five six seven eight nine so for every line it's running the arrays.two string because every single individual element of the outside array is an array itself this thing is an array itself a r of one arrow zero arr of two array itself we discussed about it a lot of times and if we want to replace it with an enhanced for loop so basically for if i want to iterate over this array the outside array if i want to iterate over this outside array what is the type what is the data type of every element you tell me i mentioned it again and again in the arr array every element itself is an array and we put the data type over here previously we were saying every element where is the enhanced for loop yeah so here the data type comes data type basically means in this array what is the type of data of every element in this case it was integer in this case it is an array itself array itself a for every array in arr just print this array how simple is that enhanced for loop gonna do the same thing just the data type will come over here okay so example if you have something like you know this string thing so you can do for what is the type of data type of every single element in the string it's of a string data type so for string element or whatever name you can give inside the array print that element iterating just for enhanced for loop okay and in this case for the outside array the main array every single element itself is an array that's why we're saying okay the data type of every single element in this array itself is an array so we did the input and we did the output as well that is pretty much about arrays more than enough for you to get started and i think i will be i will be doing separate lectures on like the questions part so after this we will be doing searching sorting and then we will be doing uh interview questions so there will be just a single video after every topic and we will in which we will only do questions so that in the future if you want to learn concepts you can watch this video if you want to learn about questions you can watch the questions video we'll be doing questions later sliding window two pointer method sub array questions you know binary search cyclic sort you know sorting and bubble sort quick sort whatever and all these things will be doing all the questions we'll be doing as well we're doing binary search on that we'll be doing tons of questions we'll do modified binary search rotated binary so there's so many things so make sure you like share and subscribe and check out the links in the description below before we move on to the next topic i can also share with you the thing i was mentioning about like why are we doing this thing arr of row.length why are we taking individual length only because it might be the case that you are trying to in you know print an array like this let's say column no fixed size okay let's say the column is not a fixed size so i can say my array is equal to something like this one two three four five six seven eight nine what if i want to print this i can just say if i from traditional way for using a for loop i want to print this i can say okay i know that there are a r dot length number of rows i know that i know there are arr of length number of rows but how many columns are there in the first one there are four so i can say for every column what is this going to be less than is it going to be less than 4 3 2 or 1 or what no it's actually going to be less than what is the size of that particular level what is the size of that row size of the first row is going to be your second row or third row is going to be array of the the what is the array at that particular row so for if the value of rho is 0 then a r r of 0 is going to give me 4 dot length print arr of row comma column i can add a nice over here and then i can add a new line over here this is the reason we were saying array of row dot length printing it very nicely first the value of row will be 0 then this for loop is going to run till the size of the array at 0 row which is 4 so it will be actually for first time it will be four second time it will be less than two so it will only be for zero and one running twice then it will be three so it will only run for column zero one and two okay that is the reason we're putting this over here now let's look into arraylist now what is an error list before we jump into what is an analyst let's let's consider why we need an arraylist so basically the idea is that um here arrays are of fixed size okay we know this is size 5 we have to give a size but what if you don't know what how much size you need what if you don't know that what will you do then you just want that hey java please handle the size for me and let me input any number of elements that i want if this is your problem statement then you can use something known as an arraylist if you have done c plus plus it is similar to vectors in c plus plus so error list is actually a part of the collection framework which we'll do in more detail when we do like the you know object printed programming and it's all it's in the java.util.packet so basically if i create arraylist example basically when you don't know how much size you want and we will also see how this is working internally you might be having questions hey kunal how is it like you know that you're not specifying any size and it's doing automatically all these things and taking as many inputs as it likes so get the syntax first syntax is basically like this arraylist data type so it can be integer let's say i want to create an array list of integers i can say list is equal to new object so i'm creating a new object of this new arraylist this is the syntax you can also write the data type over here but it's not mandatory similarly for one more thing i wanted to share was in the integer type for c people they can you can do something like this as well this is also valid but it's not like the java way to do it this is much more clean okay putting the braces after the variable name is also fine but this is looking much more nicer okay so in an arraylist example basically here this is a syntax arraylist is a class you can see here it's a class because it starts with a capital letter it does it uh extends abstracts we will turn up in object enterprising we will look into what is uh extends we'll look into what is implements we will also look deep into you know what interfaces are and what is this list and what is the serial and whatever do not worry about it for now it is just a class just focus on that okay so it's in the collection framework and uh here i mentioned in the java.util you have the you have the arraylist and it's it's in the like the util package part of the collection framework you can see over here this class is a member of the java collection framework so there are like list link list and vector whatever things that we are doing they have provided these so basically what is collection framework in simple terms in c plus plus you might have heard people say like hey with computer programming we have all these things defined already we have all the datasheets and everything written by us written for us by some people already we can just use this use this in compare programming to make our things you know make our lives easier similarly similarly there's a collection framework all the other amazing data structures like you know linked list stacks you know like your uh what is it uh hash maps maps sets and all these other things uh java provides the implementation for this in the collection framework so we will be using collection framework we will also be writing the entire data structure from scratch as well whenever we when in the future we'll be doing data structures okay that's the collection framework and one of the classes that are part of it is known as the error list class okay so this is slower than like the standard array you can input anything you like as many things as you want in it if you want to give an initial this is basically a constructor which we'll look more into you know when we do uh basically the object into programming so we look into constructor here you can pass the default size i can say let's say 10 so initial capacity 10 okay for this how this thing is working so there are two things you might be confused with right now first let's look into how we can use this then we will look into how this is working internally kunal how are you making sure that this will never be full that we will also consider after this okay so let's answer the first question first like how do we actually work with it so basically uh here when you're adding the type okay this has something to do with java object 20 programming with like the interfaces and stuff and like the generics we have a generic as a separate we have generics as a separate topic so for now just understand what we are doing how we are doing it we will do it in generics or in object entry programming i know it i say this a lot that we will do it in object and programming trust me if you see the syllabus generics is available over there so basically generics is just saying what is the type of data that you can store in this list object in this list object it's saying you can only store let's say integer if i just write string over here then you can store string over here you can definitely not write it as well it's like totally fine you can just skip this as well that is not good practice okay this is not good practice okay good practices actually write some data type that you're using so here you cannot pass primitives you have to pass the wrapper classes we talked about wrapper classes already initially but in detail we will talk about it in object oriented programming so it's like the wrapper class for the primitive data type int okay so you cannot use the generic stuff like the specifying the type of the array list you cannot use in you can't use it like this you cannot add primitives over here you have to add wrapper classes have it has something to do with object and programming we will consider that later on in the constructor construct is basically saying that all the initial values that you want to define specifically for this object in that you can add it in a constructor so this is basically a constructor being called forget forget about me saying constructor forget constructor from your mind don't worry about it we will cover it in detail in object-oriented programming for now just imagine that a new object is being created off type arrow list and this is a reference variable which is pointing to this okay that is it all right let's see how we can add stuff into it so we can say list dot so this list object has a method called add so it's saying add i can add something like uh an integer to it like right now i can add something like 67 okay i can add something like um as many things that i want i can do it again and again i can say add something like this add this add this add this add this whatever you want to add as many things as you want to add no initial sizes specified if you're saying hey kunal you have specified 10 over here okay it does not matter hello add as many one as add as many as you want let's try to print it by the way you can directly print list because when we were doing arrays.tostring whenever you print a complex object basically we will go in depth of tostring as well in programming i know i say this a lot how to string is working how these things are working why are we doing two string how can we write our own two string everything will be covered in object-oriented programming this internally calls its own two-string as well okay so don't worry about this again internally it's saying value of and it's again calling the tostring method over here of any object that you pass over here object class we will look into detail every class you know inherits the object class it's like the main top level class okay so we'll talk more about that later okay so if i print this you can see it will be able to add all these items so many all the items are added you will you are like hey kunal you said the capacity is initially 10 how are you adding more than 10 items let's see how this thing works internally so it is very simple let me just first show you some more functions and stuff so maybe i can show you something just uh i did like too many i'll just remove these quite a few just to make things simpler okay add as many as you want i can let's say initial capacity five or whatever does not matter you can print the list and you can also update something okay so adding is done so you can do list dot when you do dot you will see all the functions you can apply on this list object or of this reference variable if you want to create it to an array you can use two array if you want to add all the numbers from some other array you can do that if you want to check if something is available if you say does this contain 654 so if i say s out list dot contains 654 does it contain 6654 yes it does it will give me true true if i write something that it does not contain it will give me false okay false other thing you can do is actually change some items so i can say list dot update or whatever change or set basically yeah let's start replace i believe uh not replace all list dot set yeah so if i want to check i want to say set the 0th index to 99 for example now if i print this 0th index will be changed to 99. let's print it over here so you can update stuff as well 99 so many things you can do okay you can remove stuff from it as well you can be something like hey um let's say i want to remove the index number two so i can say remove index number 2 or whatever what is index number 2 654 654 will be removed gone very simple very simple stuff okay similarly you can iterate upon it as well you can say if i take the input it can be done now in the same way like we were doing previously so you can say for i is equal to 0 i is less than let's say 5 i plus plus you can say list dot add something like in dot next i can create in so i can say scanner in is equal to new scanner system dot in okay let's me let me just hide this thing let me hide all of these things i'm just taking the input like this and for the print part you can just do simply like this but you can also do something like this i can just say s out by the way if you can if you want to get an item so basically get item at any index for that you can't do something like this like you were doing previously you have to do something like list dot get then you have to pass an index over here i try to run this i'm gonna add five items save one two three four five one two three four five print so here here you will pass the index okay pass index here list of index syntax will not work here okay cool so that looks good and we have done like iteration output and stuff as well let's see how this thing works like you know internally and how is it maintaining the thing that we mentioned like so many things you can add in it it will never be full let's see how it works with that so let's see how it works like you know internally how the um you know errorless things is working internally so when in areas we were talking about like list of references stuff like when when we have an array of objects it's actually you know we talked about this previously if you have an array of object we did an example of let's say the string one so you were saying this is actually if you have an array of object then you have like a array like this over here in the stack you have the pointer and like the sorry the reference variable here you have the array of the object each object here itself is like you know a reference variable pointing to like the individual objects in arraylist since we mentioned not every single element can be a primitive because it does not allow it why it does not allow it we look into object-ended programming but it's the similar concept that is being followed here okay internally it's similar to what how areas work so i don't think there's a need to explain how these things is working because it's exactly the same as array but the real question is how is it saying that no size is required you're providing let's say some initial size 10 but you are able to input 100 you know 1000 elements how is that happening why is this size not fixed in reality in actual this is what happens size is actually fixed okay point number one size is actually fixed only size is fixed internally so internally the size is fixed but what happens it's when when your array list fills to a particular amount let's say for example i'm not saying this is what happens in java for example let's say your array fills by 50 let's say arraylist fills by some amount then then what will happen this is what will happen it will create a new list new array list of let's say maybe double the size again i'm not sure we would have to check of double the size a new empty list is created all elements that were in the old list are copied in the new list old one is deleted this is what happens let's say initially your arraylist had a size of three so when you input an element of four when you input an element one it will come over here when you input element two it will come over here when you input another element like nine it will come over here then you would like okay at least fifty percent is full then when you try to input an element again it will do something like this it will it will double the size it will copy these elements over here then it will input your another element that you want to add let's say 18 or whatever when this is also half full it will double the size of this again we will go into details of this why this is constant space sorry time complexity amortized time complexities is this means this particular point is for people who already know about time complexity if you don't know about time complexity skip this right now we will cover this in detail we will do the entire derivation and everything why this is on an average constant time complexity and don't worry about what is this we go don't worry what is constant don't even worry what is time complexity we will cover it in very very detail but this is just point for people who might be asking questions like hey kunal if it's like doubling the entire array and you know taking off and time and everything how can you make sure it's oh i know i know try to google a little bit but if you want the answer from me we will cover it in the space and time complexity session okay that is how it works internally let's see how actually it works in java internally so basically if i say initial size is equal to 10 that basically means like um you know that after the capacity is full or whatever it will then double it or whatever yeah basically half full or whatever let's see how it actually does it would have to deep dive into it how is the array list working it's creating a copy construction in the order they are returned from a trader there was some place where it was doing some as your release is necessary to ensure that hold at least the number of instead of minimum capacity that's true that's true yeah increasing the capacity over here here we go so this is a right shift operator it's actually old capacity and then minimum growth is minimum capacity minus old capacity increase the capacity to ensure that i hold at least the number of elements of my minimum capacity okay so here it's saying uh size plus one grow by once i'm pretty sure it's not growing it by one because that is not very efficient i am pretty sure it's like doubling it but i can't find the code for that using some hash code and stuff yeah so it's actually adding a new capacity in the grow function so basically grow is just uh taking the old capacity and creating a new capacity of the new length uh which is equal to taking what this is taking basically odor length minimum growth and some pref growth what is that this is preferred growth of the array length okay that is what it's doing all right um it does throw out of memory error like when the required array is too large so it's taking the maximum of the minimum growth and preferred growth and taking the old length and doing it somewhat like that okay so where is it grow so basically it's staying new capacity is equal to exactly what i said it's doing that so it's basically making a new capacity we actually have a formula for a new capacity so it's a little bit complex so it's saying the new capacity is equal to uh there's a function new length that returns the new capacity and whatever i said like they're taking a copy of that and adding it into the new capacity that's what they're doing that is fine but if you want to see actually how they are taking the new capacity saying the new length is equal to maximum of the minimum growth and the preferred growth plus the older length okay that makes sense sound good and it's not like you can you know definitely increases to hatchimals as you want obviously if you don't have the memory for that then it will not be happening okay but don't worry much about it they have provided this abstraction for a reason don't be like we don't understand what you are you know searching in this file no this is just an arraylist that java people have provided us i was just searching for a code like oh it's actually doubling or whatever it's doing uh it's having some own formula that is how it's actually doing it that is you know how this thing is working internally the size is being changed when a particular capacity reaches you can see it's having some preferred capacity or whatever that was it about okay you can also create like multi-dimensional array list so i can create multi array list or whatever class whatever name so here it's previously it was integer of integer array integer area of integer array this will be our list of arraylist so arraylist of arraylist of type integer of type integer nothing new i know by default uh you know for every reference value is null so if i try to print this so i will try to apply for each loop for every you know or not for each loop i just do for um int initially it's going to be empty right so i would have to initialize it first of all so how do we initialize this thing so i can say add let's say add three lists inside this so i can say list dot add new arraylist initialized so now three lists have been added if i want to now add elements into it so this is like the initialization part obviously if you don't initialize obviously by default the value of every single index is going to be null none sorry null so if you don't initialize this it will give you error for example if i say for the first because previously also you were initializing right here also you were initializing in 2d array in the multi-dimensional array you are initializing over here same thing we are doing over here so you are saying for 3 or i this is also 3 i can say list dot get get the array list at the index i and add something to it in dot next tint i can take scanner over here scanner in is equal to new scanner system dot in and then if i just print it out i can say print out list see what will be just see what this will print so if i run this it will give me some input one two three four five six seven eight nine it's printing out one two three four six seven eight nine so even multi-dimensional arrays can be printed similarly with println multi-dimensional array lists okay if i don't add it over here i will get error why will i get error very simple because it is saying hey list dot get i means get the list at the index number 0 what list this is empty what list are you talking about these are the list that we are talking about i have to actually add some lists over here okay so in what i'm saying is that for multi-dimensional array lists for example if you have something like this it's important so you know you have like add a list of area list initially it's empty we actually have to add some error list over here let's release another array list another array list another array list let me zoom out a little bit for you all like this okay so these are the things that were not created and this is the reason we were getting an error like you haven't even created any lists over here what do you mean what do you mean add what do you mean get the first list there are no lists over here that's why you have to create individual lists because individual objects okay now let's do a few questions so let's say we did the obviously we did the uh passing in functions one we can do a swap one so basically we can they swap so here i can say public static void mean integer array is equal to new integer or i can just add directly i can say 1 3 23 9 18 random numbers i can create swap it's going to take the array it's going to take two indices let's say i take index number one as one index number two as three okay what does swap do it does not return anything it will just swap here i will pass the array here i will pass the index number index 1 here i will pass index 2. the idea is just to swap this swap the indices at these two places we already know the code for swap temp is equal to array of index one area this is something we have done many times already area of index two rf index two is equal to array of index sorry temp very simple stuff now if i try to print this array it will be swapped what is it very simple nothing nothing in this question very simple question literally nothing in this question one and three will be swapped one nine comes over here three goes over here index number one and index number three swapped if you wanna swap the first two indices zero and one or the zero and the last index basically let's say 4 18 will come here one will go at the last what is it nothing we already talked about this over here passing in functions same thing is happening over here nothing new okay very simple stuff you can do another import another simple question like maximum item okay public static void main you can say integer i can just copy paste from here i'll just copy paste from here basically find the maximum item in this so i can say print the maximum item in arr let's create a function for this so it's just going to return the integer value of the maximum item how am i going to print the maximum item let's say i just assume that if we have one three two and nine let's say we take it uh you know let's say we do the code for this over here so we have something like one three four and one one three we have 1 comma 3 comma 23 comma 9 comma 8 i can just iterate upon this array i can just go one by one i can say let's say i assume that this is the maximum item let's say i assume this is the maximum item let's say i say maximum item i assume is the first element then it will compare with the next element is three greater than one it's gonna be like yes it is update the maximum item to three then the next element is 23 greater than three yes update the maximum item to 23 is 9 greater than 23 no it's not move forward is 18 greater than 23 no it's not move forward end of the loop 23 is your answer is there anything in this question there is nothing in this question another question can be find the maximum item in the range so basically here i'm running the loop from 0 to last if i'm asking what is the maximum item between index number 1 and 3 then i'm just going to run the loop from index number 1 and 3 very simple let's try it out let us try this out first let's try to find the maximum item overall so i can say something like uh from 0 till last for i is equal to 0 i is less than array dot length i plus plus i can say let's assume that the maximum item is equal to arr of 0 okay i here i say imagine that array is not empty okay if it is empty you can do something like uh integer dot min value or max min value here okay so you can take some random value basically and compare that with it let's say i'm taking the first element then i'm checking it from the other elements i'm saying hey if max value if the current value if the current value array of i is greater than the max value then my max value is equal to array of i in the end i just returned the max value what is it is there anything in this question it should return me 23 23 let's say i want to get a maximum value in a range so i want it in a range then i can create another function i can just copy this i can create another function max range i can pass a range over here in start and end end now basically i'm going to say assume that the start one is you know and then you can say start from the start go till the end nothing new that is it don't even have to run this it's very simple if i try to run this you can say max max range from let's say index number 1 till index number 3 it should give me 23 23 if i had 2 over here then it should give me 9 between this and this which is the largest number 9 so it's giving me 9. okay work with the edge cases yourself like when the array is null or empty or whatever you can just add if condition for that for yourself work on edge cases here like array being null or whatever okay so example some edge cases you can add over here is if end is greater than start so here you can just say return -1 okay then you can say something like if arr is equal to equal to null return minus 1 something like that no edge cases basically here you can see if the array is empty so you can say if arr dot length is equal to equal to 0 if the array is empty just say return minus one very simple adding the edge cases over here okay okay now using the swap function that we made we can actually make uh we can actually reverse the array now for example if i want to reverse this array okay so i can say static void reverse this array i'll just pass an array over here now i want to reverse this array how can we do this we are going to use the swap function so basically what we are going to do is we are going to have a start and end okay and we are just going to swap these two lets see how we are going to do this so if you if basically you want to reverse an array so how can you do it so if you have this array only if you have something like 1 3 23 9 and 18 have a start here and end here swap these two then move start ahead by one and below by one swap these two move start here end here swap these only so this is just a single element it will be swiped if i take an odd number of elements like 1 2 5 9 7 sorry even number of elements 8 or whatever start here end here swap these two numbers after that it will be like 8 comma 1 start will come here and will come over here swap these two numbers 7 will come over here 2 will come over here start will come over here and will come over here so it will be like swap these two numbers so this will be nine and five s end will come over here start will come over here basically just decrementing end by one incrementing start by one our loop will end when start becomes greater than and now you can't swap it because start is now greater than end and obviously this is done this is my answer reverse of the array very simple stuff oh notice you were not able to see so let me just uh here reversing the array starting ending swapping start plus 1 and minus 1 swap start plus 1 and minus 1 swap start plus 1 n minus 1 start becomes greater than end loop over okay let's try to code this okay so here i can maybe take my start and end also i can say in start is going to be equal to zero end is going to be equal to what array dot length for example but uh since here we are taking the uh you know uh index value so array.length will give me index outer bound what is the last index going to be of end what is the end index going to be in the array it's actually going to be array length minus 1 if the length of the array is 5 last index is 4 that is what we're doing over here what is the condition for our while loop breaking while start is actually less than end swap so basically i can just call the swap swap function i can say array index one is start and end just swap these two then just do start plus plus and end minus minus with the code that we wrote uh we can see that this this works both for um you know even number arrays and odd number arrays as well let's try this out let's say we do something like uh sorry not s out i'm just going to say reverse my array now when i print this it should be reversed so starting from the first one then from the second see it's rewards 18 9 23 31 18 9 23 z1 i add even number of elements that will also be reversed 56 18 9 23 3 1 this is known as two pointer method we're using two pointers right pointer one over here pointer two over here this was just an introduction if you check out the link in the description of the syllabus we have two pointer methods separately we will doing much more easy to you know lead code hard level questions in such uh particular patterns shortly in this particular segment only of the arrays let's look into what we are going to do next in the future videos so i believe that was it for this session we did some like basic stuff we mostly focused on like the theory part we did all the examples and everything we saw now the next thing is we're going to be doing some questions in a separate session that session will be purely dedicated to questions only rotation of an array you know uh armstrong of an array like uh palindromic array and then we'll do some other questions related to sliding window two pointer we'll do so many questions in a separate session just because it's easier for you to manage for example in the future if you want to learn about the theory of it you can watch this session if you want to practice some question you can watch the question session i think this is a very nice format that we are going to follow so in the next session we are going to be learning about sorting techniques and we are going to we're going to be reading so many questions related to sorting techniques and we'll be doing a separate session after the sorting techniques which is called now we'll be doing sorting and searching after this and algorithms based on that after that we'll be doing questions on like arrays questions on searching questions on sorting and uh questions on searching and sorting we'll be doing in the searching and sorting sessions only um but the questions on the arrays and the questions on like more arrays like patterns like sliding window two pointer method um using multiple for loops and other questions related to you know your sub array questions all of those will be doing as well in this particular module of arrays only thanks a lot for watching make sure you like and subscribe and check out the links in the description and i will see you in the next one bye [Music]