Algorithms for work - Array
#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;
Recommended by LinkedIn
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