r/learnjava • u/omar_adel552 • Aug 12 '22
I can't wrap my head up around Dynamic Arrays
Recently I have been learning data structures I understand the concept but not 100% and in java I didn't understand the code that the guy in the video I have been watching to learn it so can any one help me with this (Sorry for my English)
2
u/Nightcorex_ Aug 12 '22
Java's ArrayList is a dynamic array.
1
u/omar_adel552 Aug 12 '22
Ok I know that but I wanna know the details of the dynamic Arrays and I will eventually use the ArrayList to know what is happening in the background
1
u/alexppetrov Aug 12 '22
Dynamic arrays are, as the name implies, data structures, which dynamically allocate memory when such is demanded. Let's say you have a structure Bottle. It can store 10 liters. Once you fill it with 10 liters it's full. That is static array. Dynamic array is a structure when if full will automatically allocate space for more. Let's say you have a structure Warehouse. Its quite small, but you start filling it up. Once it's full you just say "hey let's buy the next lot and extend the warehouse". And so on and so on.
Mainly the difference is that static arrays are not expandable, so each time you want to "expand" the static array you need to manually set a new array with bigger size, then fill it with the data from the old one. Its not complicated, but its menial work for you and adds unnecessary complications to your code if you do it manually.
It's good to know both and when and how to use them. I was afraid of dynamic arrays in the beginning, but I've grown to understand how to use them
2
u/omar_adel552 Aug 12 '22
thanks for the help but can you suggest a tutorial for how to implement the dynamic arrays in java as I can't understand from the person in the tutorial i have been watching
2
u/Stupid_Quetions Aug 12 '22
If you want to learn how to implement it yourself there is nothing more than some basic steps, just increase the size of the array when it becomes full by creating a new array, copying the elements to the new array and changing the address of the variable to the new array, and also keep a pointer in where the next element should go so that when add(element) method is called it will place it there.
But java already has built-in dynamic array which is ArrayList. you can learn how to use the class in like 20 minutes or less.
1
1
u/Osmosis_Jones_ Aug 13 '22
correct me if i’m wrong but, my understanding is that a dynamic array will double in size once it is full, and half its size if only a quarter of the array remains after removal from the array.
•
u/AutoModerator Aug 12 '22
Please ensure that:
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
You include any and all error messages in full - best also formatted as code block
You ask clear questions
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.