Algorithms for work - Array
Nam Nguyen
? Chia s? v? nh?ng ?i?u mình bi?t, nh?ng sai l?m ?? g?p ph?i và gi?i pháp lu?n :D Coaching 1:1 thu?t toán và mindset t? duy Whatever you are not changing, you are choosing Let's connect!
#sharing
I want to borrow this picture to start sharing about arrays in Java (or any other language) and leave an open question at the end.
We somehow know that we can define an array with the ????????-???????? and the ???????? of the array.
for example: int[] array = new int[7];
And we have the name of the array holding the starting point / address of it.
and with the ?????????? we can direct access to it. But how? Okay, it's all laid down on "how you created it".
You already gave it the data-type which is the same as telling the computer that we have a block with a fixed size (in this example, it's 1 integer size of 4 bytes).
And you give it the size so the computer knows that they need to find a ?????????? for you to put the block next to each other.
So when we access the array (starting point 2000), and we want to go to the index of 3. The machine knows that you want to get the data at location 2000+3*4 = 2012. It go there, reads the data block with a size of 4, and converts the bytes/bits into the data type so we get ?? out.
Same goes for when you update array[2] = 11;
领英推荐
Fun fact / bonus: In Java we have IndexOutOfBoundsException to catch the action of accessing the 'invalid' index.
But when you work with C/C++, those exception don't happen and it still read data with the same rule.
So if you try to access array[-1] you still get the data out. So if you are using this, please be aware of it, It can OVERWRITE the data of an existing value if you modify the memory they are using.
So the idea can apply to the static array of all data types (with all kinds of sizes), and you can somehow understand direct access, which makes the array really fast when it comes to accessing and modifying the data.
Open question:
#sharingWithCS
#ChjnSu
Love this topic