1. Home
  2. Explore
  3. Computer Science
  4. Java Data Structures and Algorithms

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Java Data Structures and Algorithms

Transcript

Data structures and algorithms in Java are not just a skill. They are the foundation for building strong coding careers. Whether you are aiming for back-end development, machine learning or app development, DSA is what helps you think like a true problem solver. Setting you apart in a fast paced tech landscape. This video is designed to walk you through these essential skills, helping you make sense of foundational Java concepts with ease. Here's what's in store.

First, we will explore the core data structures like Arrays, Stacks and Queues with hands on code examples. Then we will move to more complex structures like trees that help organize data effectively. We will also cover essential searching algorithms from linear to binary search and introduce you to greedy programing. Finally, we will dive into graph algorithms like breadth first and depth first search bringing each concept to life through practical coding examples.

By the end of this video, you will come away with a solid understanding and hands on experience in DSA. So let's dive in. Now let's talk about what is data structure and why do we need data structures. So as you can see, this word data and structure is made up of two words data and structure. So data is nothing but the information that you want to store in a computer. And structure is how you arrange your data in the computer. Now, depending upon these two factors data and structure, that means how you arrange your data in your computers. You can easily calculate time, complexity of your problem,

and you can also see are you using some memory depending upon the structure that you're using. Now, if you talk about it, let me give you an example of one of the data structures and compare it with another. As we talk about arrays, it's a data structure and the way you arrange your data is different from how you store your data, when you talk about linked list. In arrays you have to always store data in a continuous memory allocations wherein if you talk about linked lists,

if chunks of memory are available here and there, you can you can use those chunks of memory to store the elements in the form of linked list. Or you can use linked list at that point. But if four bytes are there or bytes are here in different different places still you can use linked list, but that is not the case and you need contiguous memory allocation. Now, in both of these data structures, one of them is using CPU much more efficiently, it is using the memory much more efficiently.

So this is one of the basic difference between the data structure, between data structures, right. There are n number of data structures available and different data structures are used for different applications, right? Depending on these factors like how you are storing your data in the memory, you can easily calculate what the efficiency of your algorithm how efficiently you are implementing your algorithm, how efficiently you are doing operations, right. You are doing operations which are costly or are you doing operations which are which takes constant amount of time? Are you using your memory efficiently? Are you using your CPU utilization efficiently? Are you using your CPU efficiently or using your memory efficiently? So all these things can be easily deduced from what the structure you are using. So this is the importance of data structure. Now let's talk about in the next slide, let's talk about what

different data structures, are there any different types of structures. Yes, there are two different types of data structures. One is a linear data structure and another is a non-linear data structure. Welcome to this course, which is about Java Data Structures. So before going into the detail of all the topics here, let me tell you what all we are going to discuss in this session. So we'll be talking about what is data structures and then what are the different types of data structure. Starting from the most widely used data structure that is arrays, we will go till the graph data structure. So for every data structure that is array, queue, stack, linked list, we'll be talking about the basic concept of these and then implementation of different operations which you can perform on these data structures. Their implementation will be discussed using Java programing. And then advantages and disadvantages of using each data structure.

That is a stack you long list will be discussed as well. Then moving ahead, we'll talk about rewrite structure and entry. We'll talk about Binary tree as well as binary star tree. Different traversal mechanisms over binary size tree will be discussed. And lastly, we'll talk about graph and hashing. So in graph data structure, we will learn what are the different sorting mechanisms over graph data structure. And then moving ahead to hashing will talk about what is the basic concept behind logic, Why is it used and what are the different of these different packages which you can use from Java programing to implement the concept of hashing? Now let's talk about what is data structure and why do we need data structures. So as you can see, this word data and structure is made up of two words data and structure. So data is nothing but the information that you want to store in a computer. And structure is how you arrange your data in the computers.

Now, depending upon these two factors, data and structure, that means how you arrange your data in your computers. You can easily calculate the dying complexity of your problem, and you can also see using some memory depending upon the structure that you're using. Now, if you talk about it, let me give you an example of one of the data structures and compare it with another. Now, about a slide. It's a data structure. And the way you and your data is different from how you store your data.

When you talk about interest in it is you have to always store data in a continuous memory locations wherein if you talk about link list, if chunks of memory are available here and there, you can you can use those chunks of memory to store the elements in the form of link, or you can use link list at that point. But if four bytes are dead or bodies are here and that are different in different places, still you can use link list, but that is not the case and that is you need contiguous memory location. Now in both of these data structures, one of them is using CPU much more efficiently, is using the memory much more efficiently. Right. So this is one of the basic difference between the data structure, between data structures, right? There are any number of data structures available and different data structures are used for different applications, right now depending on these factors

like how you are storing or data in the memory, you can easily calculate what the efficiency of your equalizer, how efficiently you are implementing or how efficiently you are. You are doing operations, right? You're doing operations which are costly. Are you doing operations which are which do we should we states constant amount of time. They are using your memory efficiently. Are you using your CPU utilization efficiently? Are you using your CPU efficiently or using your memory efficiently?

So all these things can be easily deduced from what the structure you using. So this is the importance of data structures. Now let's talk about in the next slide, let's talk about what different structures are going to need, different types of structures. Yes, there are two different types of data structures. One is a linear data structure and another is a non-linear structure. Now let's talk about linear data structure. So linear instructors allows us to store data

in a sequential manner like one after another. And talk about this example where we are using a as a linear event structures where one element let's suppose this is five, six, seven, eight and nine and one element is stored and it is stored after the next element and next element is stored after the next element. So it is stored, all the elements are stored in a sequential manner. Let's suppose this is the end of the memory. Obviously this will be a hexadecimal number for simplicity.

I'm taking this number as an Indigenous and then the next memory, even in memory equation, is one of more than 118910112. Okay, so these are the available memory locations and all the data is arranged in a sequence in a manner and every element has two us right to talk about this element. Right. It has two neighbors, the left neighbor and the right neighbor, and they're always predefined extremes. Like if you don't have any, it will be a starting in NICS and it will be an ending index.

So this is always the case when you talk about lenient data structures. So let's talk about the data structure that is there, which is known as link. Let's try again. It will have a predefined extreme right. It will have a starting node that is known as the head node and it will have the day node, which is used to keep the track off that end point. So now let's talk about different types of data structures, right? That is linear structure, right? The first one is the ETI, which is obviously the linear data structures and all of the elements are stored in a linear fashion. Now this in this data structures, we can store data in a continuous memory allocation that means one after the other in memory. If you have the scenario where all the memory is occupied and you have chunks of memory, let's suppose you required for for four and you require let's suppose you require photos of really right. So many bags of memory are required.

So you have eight bytes here and you have 12 bytes here. Again you stored this this any inside these memory locations like let's suppose you are storing will MP can you do it. No. Because edited were going to be used in the allocation. You cannot store it now what was the drawback that obviously you can also add it at that location so that is when link list coming to picture. Obviously with any you have a fixed big state that is not the case in linguists. The size is variable.

That means we have a dynamic size in linked list. Now if you double are in English, if same elements, same elements need to be stored in the memory, like these 20 bytes of memory are available. I did by label here and we will here. We can store elements in the link list. Right so it does not require can be used memory elongation. Right. That was a drawback. And lindis and I'll stack it we talk about it photos The principle of one lethal last imposed out of the stack of plates right is an example of said you have a stack of plates one piled up over the other. Now if you want to add an element in this stack or if you want to remove an element in the stack, you cannot remove any of these elements unless and until you have removed the top. And then one by one you can go down and one by one. These elements right now similarly cannot add. And you cannot adhere to an element because that is not possible.

It follows the structure of it and last in is the first out and suppose you want to insert it, you are inserting it here. This is the last element that was inserted. Now this will be the first element that is out. Okay. So it follows the principle of me for now. This has restricted how we insert and delete elements. Right? This is a restriction that is there. So this is your stack. Okay. Now who told you? It works in a different manner. Now, if you have a queue, let's suppose you

that you are there in the bus station and you want to get tickets. So you're standing in a queue, right? And the person that comes after you will stand behind you. Let's suppose it is me and the person that comes after me will stand behind me. You let them go in front of the queue. That is not possible right now. And once this person gets his tickets, this will be removed. I'll this one will do the first person again. This arrangement is only the was chosen and so on. So there are two. Point is that when you keep track of the extremes, the friend in the front part and the rear part now from the front and always deletion date place. It's also known as an in operation. We'll talk about these operations later on this course. Okay. So now the deletion will take place from the front and insertion will take place from the rear. So if you want to add an element to add it from the rear side, that means this site you're adding the elements. This is your grave site. Okay.

You want to delete any of the leading from the front? Let's suppose this is five, six, seven, eight, nine, ten. Now, now you do have 11 and then and still you have many locations. So 11 now you want to add, you will update your rear and it will go there. And if you want to remove, you're going to remove this element and you'll increment your friend point to this location. So this is how you are. You walk around with you. So obviously with these two they're stack and we can implement these

all these data structures either using arrays or either using link list or again, both of them. So obviously there will be some kind of difference in the way you are implementing it. Obviously, with any use you have fixed size with English, you are dynamic size ladies. Now you have to store Bendigo's me locations, right? And with link list, even if there is no contiguous memory location, you can still use your memory very efficiently. Right. So this these are some linear structures and we will go all of these linear data structures in this course. Now let's talk about nonlinear, the structure is a nonlinear data structure that allows arrangement of data inconsequential matter that again and neighbors for a single element, right? So let's suppose this is nonlinear is just about binary tree or we are talking about any rate tree means there are no restrictions of how many this loop will have or this element will have or this

not that it will be specific this node will have. So there will be any number of details on this non. So there is no restriction for the next node. It can be two. So this is what is written. They can have an numbers, they can be any number of neighbors for a single element and they do not have predefined extremes like we saw in a linear structure in the extreme right. I have a friend in our rearranged solving in case of stack. Right? And you have in case of A's you have to define a size is of long list. You have a head and a date. But here you don't have any extremes. There is no extreme in this in this nonlinear structure. You can store them as you want and it is not specifying that every node will have extreme say so. Similarly, if you have if you talk about binary trees now we have a strip string of number of chains that each node can have. So let's suppose this is ability. So this known at Max can have

two JS, one on the left, one on the right. And this don't have at Max either zero day one jail or two Jane So at Mercy again how do children's like in this case it can be empty or it can have one less J or it can have the next node can have a different R right, a single rating. So this is a line. Now you can even restrict it further, right. By arranging these data in such a way that you always stored on the left hand side, you always store the lesser element, then the loop

and on the right hand side you always need to have benches in the right. So these are different given variations. Now let's talk about different types of data structures, nonlinear structures that are there. So if you talk about binary tree, right, this is a restriction of storing your data in a hierarchical fashion. So you will be storing your data. And I think fashion, like every note, will have either zero one or not. I would have left J It will have a right shape or it can have

this node can have this node is having future inside this known as having zero zero and this known is out right? Jake that it has only one J So this is a binary tree really. Every node at Max can have children right now. We can even restrict it further by saying that you can always have and I suppose the data stored here is ten, so you can always have elements on the left hand side that are greater that are less than ten. That means you can have any number of elements like they will be restricted in such a way that all those elements will be less than ten. So this is your BSD binary substring and all the elements that are greater than and that means 1112 1340 will be on the right hand side and all the elements are less than ten, right. Five, six, seven will be on the left hand side. Now then we have graphs right again, a hierarchical data structure, then we have aviaries, then we have radionuclides.

So all of these data structures are nonlinear data structures. So what is an 80? It is a linear data structure that means elements will be stored in a linear fashion, a linear fashion. Now, if we talk about a mistake, an example now let's consider that this is how you represent dignity in the form of control. And they suppose it contains elements one, two, three and four. Like now, with every memory location, there will be some errors. Right? So let's suppose these are the four elements, right?

One, two, three and four. And these are some accuracies, let's suppose is 100 and this is one of four. This is one eight and this is 112. Now, if you talk about memory, obviously these addresses would be hexadecimal and in this particular area it would be somewhere in the memory would for audience here, for bytes of memory, for each India. Now if I take out if I could say it was integer and let's suppose MDG takes four bytes now these four bytes are available for each integer.

Now this is an integer right now. It takes for by now the second and this will start from one or four, right? Because now again, this will be for bytes. Then one again, it will be four by ten, 111. So in memory it will be somewhere at all. But the thing that obviously you might be thinking, okay, so let's suppose this is our memory and now if we have four and four, by that means eight bytes here and eight bytes here, but they are available in chunks, right. This one junk and this is the same gem and rest of the memory is occupied again, we stored this any in your memory. In the memory. Not because it needs contiguous memory allocation. That means when this is the scenario where in you have memory locations are memory locations available in a one big chunk. Right? That means if you talk about this array, it requires forwarding to four that its 16 legs are available and they are available in a contiguous memory. Fashion night

ought to be available in such a way that it is a one single junk all 16 bytes. Okay, so then only you can stored the elements at that location. Now obviously for simplicity I'm digging this addresses as a number and digit number, but in reality those are hexadecimal numbers. Okay. So it is easier for me. Okay. So now one more thing is that the elements are stored in a linear fashion, right? But can we access elements randomly? Yes. With the help of indexes.

So if you talk about this, Eddie. Right. One, two, three and four, obviously this there will be a name associated with this. Eddie. Right now we have indexed 013. Now why indexing stocks the zero or why there is a zero and indexing all of these stocks with zero null, which is that right? And I always try to demystify this fact that why indexing starts from zero, why not? It starts from one. Now, if you remember, I told you that there will be a name associated with this Eddie that is the IRA.

Now, this area is nothing but name of the Eddie and name of the Eddie. The presents. Right. It represents. It's the entries right now. The basic resolve. This was earlier we spoke about it. So it is unknown. This is one of all. This is one or eight. And this is one one. Okay, So now this is our moment. Now let's talk about how you exist. We will talk about in the coming slides. We will see how declare and each lays on Eddie or like suppose we talk about how to access this, we use Eddie

and then the subscript and the name next will be the indexes one. Now, I told you the name of this Eddie represents the lives, interests, all these interests now plus one now. This one represents four bytes. Okay, so the four bytes then what internally happens? It would be it was down to 100 plus four. That means one or four. Now, one, two, four is not the post location. It is the second location. Okay. Now, similarly, if you talk about accessing, the second element, could in theory it was down to one Eddie of two, which is nothing but a hundred plus two. Now this two is nothing but eight right under one way. So you will be accessing the third element in the now how can access the first element? So you are all zero now. It was 100 plus zero because there are no bytes, right? So it was down to this that Eddie indexing starts from zero and now you know why and this is how you get access elements randomly. So with the help of these indexes.

Okay so now you might be thinking okay now we have in Eddie can we stored different elements right. And we store and suppose is that it can we store in this was is for integer then we saw a floating point number and we was again we saw a character. Not if you talk about any particular Eddie let's talk about this Eddie Now the data type or the type of data that you can store in this, Eddie will be homogeneous. That means you can only store similar elements. Okay, so these are

some facts, and this is how Eddie works. And what are the addresses? What are the indexes? Can you store different elements? No, we can only store similar elements in beginning notice. Talk about the applications of Eddie. Now you might be thinking, so why do we need this? Eddie? What is the what is the reason that we are using this? Eddie So basically, when you talk about Eddie's now, obviously when you have a static or wherein you want to store all your elements in a linear fashion, right?

And that do you want to store them in a contiguous memory locations, right? So that you can use your CPU or you can use your memory efficiently, right. Not the CPU, you can use your memory and you can utilize your memory to the maximum, right? So you want to utilize your memory efficiently at that time. You can use this, but obviously it will has some drawbacks. It has some drawbacks. That is why we have different data structures, right? So if you want to store your data in a media fashion, you can use Eddie's okay. Now. It is also suitable for all the scenarios varying you require frequent searching by. If you want a certain element in an Eddie, you can directly go and access these indexes one by one. Right. So in a linear fashion you says, okay, is this the element that you're looking for? No. Is be the element that you're looking for know is 30 them and that will be important? No. As follow them.

Yes. One by one you're going to access all of those elements and try to search for the element that you are looking for. Okay. So it is suitable for applications which to what begins searching now is not about one dimensional. So if you know the world one day it is it can be related to or like we saw in the example, right? So that is what is a one dimensional Eddie It is represented in the form all through and we have accuracies like one 401

for 18112 and 116, and indexing will be obviously 0123, four. And then there will be a name associated with this area, which is ERA. And then again. So all the information this area suppose it the it is an integer really okay you can sort only integer elements and the size of the city is five and you have stored the elements one, two, three, four, five. So now it can be related to it. All the elements are stored one after the other, like you see you have one, then you have moved any other three.

And there's those. All of these numbers are in a contiguous memory, are available in them because now when you talk about one B any only one index used when you try to declare an insulation area that time you will use defined subscript. Okay. So how you can use that? Let's suppose if I what if I talk about the special area that I that I would define it. You will define it area and then the sub the index is that the number of elements that are present here which is five.

So only one subscript will be there, one index will be used. Okay, so this is how you declare your ETI. So now let's talk about the declaration at initialization of this. So obviously when you talk about this, there will be a name associated with the ETI and then the data. Are you going to store integer values in that, then the one that one subscript or one index that we use and then definitely the size of the A, So this is how you declare your one the any. Now organic insulation.

There are different ways of getting insulated. Obviously here you are getting it. Then you might use a for a loop which is all the elements, all you can declare on initialize your ETI at once. So how do you do that? So you are right and it's supposed to design rigidity and you don't have to specify the size entirely. Right? The elements and those elements, let's suppose those are one, two, three, four and five. In this case we are getting and glazing your any ones at that time, this size becomes optional. You don't have to specify explicitly the size of the thing, but obviously the size of this area will be fine. Okay. Now since you are declaring and initializing it at once, so this is optional. Like in the case where we are in, you are not internalizing it at that time. The size becomes very important and you have to mention this size explicitly. Okay. Now this is not about two dimensional. And so also it is known as 2D.

And so it can be related to a table like this or you can also see a matrix wherein you will have rows and columns. Right now in this element are still one after the other in such a way that you can think of it as a1d And now this is what when did it right as we have already seen it right And so in this one and the any you have another one very right so this is known as to the edit So now how it works right now let's suppose you have numbers on here and forward numbers one, two, three, four.

Then you have five, six, seven, eight, nine, ten and 11. And so this is a 2D edit of how all and this will be similar to what of having three rows. Right. You have you will have three rows, not for rows you would have beetles and in that in those three rows I would have what four columns. One two. And so the numbers will be like this. One two three four, five six seven eight nine ten 11. And then so now obviously, this will have let's suppose this is a zero based indexing

and this will have a zero index here, one index here. Now, internally what is happening, it will be a00 and it would be a01 and 0203. Then 10111213, then 20212, two and three indexing. Right. So similar to that we would have this index would be zero, zero would be like this and this would be like this. So if you want to access the element that is present at this location, what you will do, you will run to 4.81 will be starting with from here, I suppose one will start from a zero

to the length of this. I'll go ahead and that is three less than three. Right. So and the another from zero two. The number of those that I did. Okay, so this one will be the outer loop. This one will be the outer loop and this one will be the thing that means the number of columns that got it. So this is four and this would be for column that could start from zero to less than four. Okay, So this is our library and it will be similar to this.

And now what about the edges right on about the addresses that we did. So this will be let's suppose if this is 100, this will be one or four, this would be one or eight, this would be one, one, two, this would be 116, because internally it is treated as if they are again in a contiguous memory location. But this time around we have a one day and inside that one day you have another one. So for declaring it, you will use two subscripts. So it will be something the name of the ending, then two subscripts. And now this will represent the number of rows and this will represent the number of of them. So in this case, the number of rules to be three and in this and the number of columns is four. Right. So this is how you declare quite a trinity. So dimension depends upon the number of subscripts you are using. So this time at all we are using tells us Now let's suppose you are using three subscripts, right?

So similar to like this three, three and three. Okay, so this time around this is 3D. And similarly you can have multi dimensional and we just need to keep on adding the subsequent X6. Now let's talk about the implementation of arrays in our programing. So before getting into the idea and writing down the code, let's try to see what is the problem statement. That is what code exactly we are going to write to learn. Implementing that is in Java programing.

So the problem statement is that we have to write a program in which we are going to create one dimensional, adding it will hold integer values and those integer values should start from one and store the maximum number it can fit into meaning. Suppose the size of the array given is ten, then it's a stored number starting from one. Think that and also we learn how to access those numbers once it is stored in a one dimensional area and also to display them as an output.

So this will be the first part of the problem statement. And then we'll talk about how to create a two dimensional integer editor and how do a stored number and do it integers into it starting from one to the maximum value we can store into it. And as we know, two dimensional it is a matrix. So it will start from foster and first column and then keep on storing the numbers going across each row. Tilde the last row and last column. Once the two dimensional arrays created will be again writing down the code that will help us access those elements. And then will implement the search and start operation that this will be sorting down in ascending order, each element of the one dimensional and two dimensional areas as well as will implement search on the given eddy. Now let's switch to IDE and implement this code. So I'm using Cliff's idea to implement this Java code

and I've already created a class that is a resolution and there is a main method public static word me string args now head itself inside the main method. We are going to write down the code that will help us create a one dimensional array first. So as we saw the problem statement had two parts. The first part was to create a one dimensional lady and X is elements of that one dimensional dimensionality. So how to do that in Java? Let's try to see that. So first you have to to define or declarative one dimensionality to define that, you have to give a data first using the square brackets. The square record represents that. It is a one dimensional one square record, meaning one dimensional array to that two square bracket, meaning two dimensionality, then space and the name of the array. Suppose even I want to give the name of my edit. Okay, then after this will be using new keyword. New keyword. Angela helps us to create any object

for a given class, or it has to allocate space in the memory. Okay, so new. And then again ink. That is a data for the array. And in this square bracket we should define the size of that one dimensional. So here, suppose I want the size of the one dimensional error to be six and then square record will be closed. And Michael So this is how we will create one dimensional. Adrian Della Now how to store elements. Now comes the point of storing the elements or numbers into it

as it is an integer and it will store numbers, that is integers. So I have the store integer starting one to the maximum value it can store. So to store the element, there are two ways. Either you can refer any integer by direct the index number of it. Right? So the index it area starts from zero and goes to the maximum. Suppose the size of the array here declared a six, so it will go from 0 to 5. So either you can refer like A1 and then you can use zero as a zero

the index and assign some value, hit four and then say make this way, you can store it for each index that is A1 zero will store it and the number may be five. But as our problem statement says that we have to store numbers starting from one, then the maximum value it can store. Right? So for that it will be easy for us to any looping condition. So Della has so many constructs where we can loop around a given code. Okay. One of the constructors your for loop so we'll be using for loop here.

So this index so far Lopez for keyword then any variable which will be using to iterate over a given edit. So it is short start. From that I should start storing numbers or the gist of it from zero to index. So the value of I will be zero because I use a variable which I'm using, but iterating over there then that would be semicolon. Then the next condition in the loop is stopping condition little where this I should iterate over it to starting from zero and then should it end.

So that ending condition should be that the second barometer for for. Okay so I should end at the length the end of the array have to store. Right? So that is the length of there. So there is a function in Java that will help us create or calculate the length of a given that even dart length. Okay, this is the exact length I have given Liz inside here because the length will be six. That is the size of that. But as I told you, index starts from zero and it will go till five. So we do not store that sixth place, right, because six doesn't exist. You're just starting from zero. So it will like if it starts drawing wonderful. Jordan six. Right. So that's what I should be less than only Erin Portland. Then the next barometer of all of this by what you want to increment I every time. Okay you want to go to the first element, then second and third and fourth or you want to go to first element and third directly or fifth directly.

So that condition we have to put as a third here. So this will be I want to go just like increment one one each thing. So I plus plus so I plus plus meaning I will increment each time by one and start from zero will implement one to go to one, then two, then three and so on. After that you have to go for loop. Golly brackets. Okay. This implies the body of the value in this. Now I'll write a code to store the element into it. I Want to store in one eighth location? I want to store what?

Okay, I'm using a variable over here. Count. Okay, this count begin. Initialize it here itself as count is equal to zero. So this count and start from zero. Okay. It should be of type int because that is what we are going to store in this area. So this it will start from zero. So when I is equal to zero that is at zero it index of 80 it will store the value of count which is zero initially, then it will increment I then second location it or store two.

That is I should increment count every time. So I'll increment okay. As it is will can started from one because I want to store number from one to an okay one to whichever maximum it can store. So do start from one and then every time I'm going to do count plus plus it means it will increment count so it will store one first, then a second place, it will increment itself and store to then increment itself and store three and then for random. So on the last place in the edit.

Okay, so this is how we'll be writing down a code for storing numbers into a given one dimensionality. I hope this is clear now. I'll come out of this for a loop. We have to access the elements of this one dimensional two axis and display what we'll do again here for use for a loop. Every time to iterate over to 80, we have to use some looping construct. Okay. And I'm using for loop over here. So again, I'll go from and maybe J this time you can variable also no issues, but I'm taking different variables. So J will be again less than or even at length. Okay. And semicolon J will increment every time with one and in this I have to access it. That is I have displayed so I can do system dot output println. Okay. So if you type and press control space case in eclipse ID, it will write the full form of it automatically. So system node outer println. And then here I should print work even off day every day. Thanks for having displayed.

And as I have given here, print a little statement. It will print one in each line. If I want to print all in the same line, you can do like this plus and then you can give a space to it. So it will give us space between every number and it will be displayed. Save it and execute it. So once you will execute it, you will see the numbers has been stored starting from 1 to 6 because six was the size of it. So it started from one and stored it six. If you want to change this, you can do suppose eight is a size. There won't be any other change. You will just change the size here and the four loop will take it to the end of the edit. That is in the length of the idea from 1 to 8. Okay, so this is the implementation of one dimensionality. Now coming to the second point of our problem statement, and it is that was about your two dimensionality. So once you're done with two dimensional, then I'll talk about a certain sort

over this one dimensional genetics. Now I will come in this line here. It okay and we can go ahead and from here we can write down the code for two dimensional arrays. Here I'm going to write the issue and then an implementation of 2D. Okay. So what you have to do here, this same thing beta type, that is which kind of data you want to store it in the two dimensionality. Then as it is a two dimensional array, it should have two square brackets. Okay. Then again, the name of the area.

Suppose you win again, same name, I'm going to give you it and then equals to a new operator to allocate space to it. And then again the, the type of the array that you want to create and then again to it is you it I mean two square records here you have to provide the size again guys. So let's have three and four. So three rows, four columns matrix or a two dimension A will be created. This is how we create a two dimensionality to access this, as it are not just a single column, but a single root area, columns and rows. So we need two variables to iterate over it. So first variable will be used for iterating over the color and then the second one for rows. All first one photos and second one for column depends upon how you write the code, so it will start from zero in the any matrix, the first value is zero true and zero column. So don't start from zero itself. So it will go ten error naught length, right. And then increment every time by one.

So eight plus plus, but now this has to iterate over the root note we read over the column. We need one movie. So one more for loop. Let's take it as Gee, suppose starting from zero again zero eight column it will start and it will go this time to each row. It has to start from zero and golden and the next row start from zero. Go to the end of that particular row itself, getting it so it will go down the length of each row, first, first row, then second row, third row four through this way.

So every time it should start from zero and to end of each row, getting it so it will go down. Read E one of I thought length and then semicolon and it will increment every time only one value. So Duplass press getting it. So you have to be very careful here with the ending condition. That is where the loop should end. I will start from zero and it will end at e one at length and David start from 1 to 0, but it will go till end of each row every time. First row, second row, third row.

That's why it is e1i of length clear. Now in this two axis are two value. So first let us store the values, then only we can access it and displayed to store the values. The syntax is again c just that you have to use a square bracket and then G square bracket and equals two will store some value here. Let it be a variable column. So you have to again initialize also zero count, let it initialize to one. And as I want to store a number starting from one till the maximum

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent