 In this video, we're going to explain how arrays are stored in memory. So arrays, or also another way to refer to this data structure is called tables, are memory structures or data structures that are made out of a table with multiple elements, number from element 0 to element n minus 1, assuming that the array has n elements, and let's assume that each of size k bytes. So the interesting thing this table has, let's suppose this table is called table. The first element is here, the second element is here, and so on and so forth until the last element, which has the number n minus 1. The special property about this table is that to refer to these elements, we're going to use the following notation, we put the name of the table, and then in between brackets, we'll put what we call the index, which is a number between 0 and n minus 1. For example, table of 3 refers to the fourth element, because the first element typically in most programming languages is table of 0. So basically what this data structure allows us is to have a collection of elements, all of them of identical size, so they're all the same type, one stored next to the other, and referred to them is using the name given to this table, followed by what we call the index. Now let's see how this data structure would map into the memory of a computer system, and it's actually fairly straightforward. Let's assume this is the memory, and let's pretend that this data structure is a store beginning at the address 100. In other words, the first byte of the first element of this table is a store in position 100, then 101, 102, so on and so forth. Since we know that the table has elements of size k, the following element, this would be table of 0, will be stored in position 100 plus k, and we refer to that element as table, or whatever name we give to our array, on. Now as we can see, for the elements down the table will be all stored in positions that we can obtain by adding k bytes to the previous one. So in general, the element table of i, being i any number, will be stored at the position 100 plus i times k. So a different way of looking at this is that if we have this data structure stored in memory, and we know the beginning of the data structure, the address after which all the bytes of all the elements are stored, whenever we access to a specific element, the address of such element is immediate, because what we have to do is multiply the index, in this case would be 3 times k, and add the result to the initial address of the array, in this case 100. Now, think carefully about these notations. This is a generic notation, meaning independent of the type of data in the array. Very important. What does that mean? It means that if this array is an array of integers, then each of these boxes will be an integer, and if those have size of 4, then the addresses of these table of 0, table of 1, table of 2 will be 100, 104, 108, 112. However, if these elements, instead of being integers, are complex elements that require, for example, 50 bytes each, then the first element will still be stored in position 100, the next one in 150, the next one in 200, which is 100 plus 2 times 50, so on and so forth. So this notation that we have described generic, it is independent of the type of data stored in the array. Now, when we manipulate this type of data structure, there is an issue that we have to be aware of, which is how do we know the size of this array? In other words, suppose that you are told that there is an array that starts at position 100, and suppose you have to traverse the array. Suppose that you are given the impression that all the elements have size k, so you know that in order to traverse the elements, you have to jump from k to k bytes. However, how do you know when to stop if you are traversing this table? In other words, how do you know the size? So typically what happens when you manipulate arrays in memory, you need to know two parameters. The first one is the address in which it is stored, and the second one is the size or number of elements. And of course, you also have to know the size k of each of the elements. Once you have these three parameters, not two, but three parameters, address, size of elements, and number of elements, then you can compute the address of any element in the array. Now let's follow an example and see how arrays are stored but in Java. So we're going to see a specific example of a programming language. For example, the way you declare an array of 20 integers in Java would be like this. So this is directly Java code. Now, how is this array stored in memory? Well, it just turns out that Java or the implementers of Java realized that you needed these three parameters, but one of them is very important, which is the number of elements you have in the array. And that is something that you need to check continuously because in Java, if you do something like t of three, obviously is a correct access to this array. However, if you do t of 23, then you get an exception, which I'm sure you're familiar with it, because it's the array index out of bounds, which occurs fairly often. So how does Java know that 23 is an illegal index for this array? Well, very simple. It compares 23 with any number in brackets here. It should be between 0 and 19. And the way Java then stores this data structure in memory is following this strategy. This is memory. This is the first position, the first address of the array. Let's just assume, for example, there is position 278. That's the initial address. Now, instead of storing the elements one next to the other, immediately at this address, what Java does is it stores first the size of the array in a cell that occupies four bytes. In other words, in this example in which we define an array of 20 elements, that number 20 would be stored precisely in this location. And as we know, integers in Java occupy four bytes. Then after this, in other words, in the address 282, which is the address you obtain, adding four bytes to this address, four positions, starting on 282, you would have the element t of 0. And since we are considering for this example an array of integers, the integer occupies also four bytes. And therefore, one would be in address 286, all the way down in memory to the last element of the array, which would be, in this case, t of 19. Now, with this structure, remember, the first position of the array contains an integer encoding the size of the array. Now, if I'm given the expression ti to access, the address of this element in Java, it's computed, following the formula we've seen here, but with one little correction, which is we have to account for these four bytes that are at the beginning. So the address of ti would be the address of the initial element plus four plus times i, where k, if we are storing integers, is four bytes, i is the index, this is the initial address, and these four over here that we have to add is because we store the size as integer. So in this example, this of the position t of 10 will be computed as initial address, 278 plus four to account for this cell here, which contains four bytes, plus 10 times four because I'm storing integers. And therefore, the address in which t of 10 store for this array starting in position 278 is 278 plus four plus 40, which is 322.