Dynamic array source code

Easy to Advanced Data Structures Static and dynamic arrays
6 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
This is a free item
$0.00
د.إ0.00
Kz0.00
ARS$0.00
A$0.00
৳0.00
Лв0.00
Bs0.00
B$0.00
P0.00
CA$0.00
CHF 0.00
CLP$0.00
CN¥0.00
COP$0.00
₡0.00
Kč0.00
DKK kr0.00
RD$0.00
DA0.00
E£0.00
ብር0.00
€0.00
FJ$0.00
£0.00
Q0.00
GY$0.00
HK$0.00
L0.00
Ft0.00
₪0.00
₹0.00
ISK kr0.00
¥0.00
KSh0.00
₩0.00
DH0.00
L0.00
ден0.00
MOP$0.00
MX$0.00
RM0.00
N$0.00
₦0.00
C$0.00
NOK kr0.00
रु0.00
NZ$0.00
S/0.00
K0.00
₱0.00
₨0.00
zł0.00
₲0.00
L0.00
QR0.00
SAR0.00
SEK kr0.00
S$0.00
฿0.00
₺0.00
$U0.00
R0.00
ZK0.00
Already have an account? Log In

Transcript

All right time to look at the dynamic array source code. This is part two of two in the array series. So the source code for this video can be found at the following link github.com slash my username slash data dash structures. Also make sure you saw the last video so you know what's going on with this array implementation. All right, here we are in the array class. So I've designed an array class to support generics have type t, so whatever type of data we want in this array, that's fine.

So here are our three instance variables that we care about our which is our internal static array. Len, which is the length the user thinks theory is and capacity is the length of the actual array, because sometimes our array might have more free slots. And we don't want to tell the user that there's extra free slots that are available. That's just our internal knowledge. So there's two constructors. The first one will initialize the array to be of size 16.

The other one you give it a capacity capacity, of course, has to be greater than or equal to zero. And then we initialize our array and cast it to a type T. Also notice that he put this suppress warnings and unchecked just because of annoying generics in Java. So here are two simple methods size, get the size of the array and check if the array is empty, pretty self clear. planetory similarly, forgetting sets, we just index into the array if we want the value or set it if we want to set the value, technically I shouldn't be doing and a bounced check for both of these, but I'm not apparently. Okay, clear. So here we just remove all the data in our array and reset the length.

Simple. This next one is the Add method where things actually get a little bit more complicated. So this condition says, Hey, if the length plus one is greater than or equal to the capacity, then it's time to resize my array. The way I'm resizing is I'm doubling the size of the static array. And you don't have to double the size, but I've decided that doubling the size is convenient. So When I double the size, I have to create a new array with the new capacity.

Copy everything in, this is what this line or these lines are doing. It's copying the old values back into this new array, and then it sets the old array to be the new array. And lastly, here we just copy the new value into our array. So this remove ass method will remove a particular value at a given index. First, we check if the index is valid. If it's not throw index out of bounds exception.

Otherwise, grab the data at the Remove index. initialize a new array of size length minus one now cut copy everything into the new array, except for when it's at that remove index. Now I'm sure there's easier ways to do this, but I decided to do the following maintain two indices i and j. increment i and j as you go, but when i is equal to the Remove index, then we skip over the Remove index by fixing j temporarily and using j to lag behind, if you will, while is still in the original array. So I guess pretty clever overall. And then set the array to be the new array we just generated. Reset the capacity and return the data we have just removed.

So this next one already Move we scan through the array. If we find the object we're looking for, remove it at the index and return true otherwise return false. index is very similar. If you find it return I otherwise return minus one. Contains just checks if index of is not equal to minus one. All right, this next one, I return an iterator for the array.

So an iterator allows us to iterate over the array, providing an abstraction for it. So I need to override two methods and this is has next. So there exists the next element in our array if the index is less than the length of the array. I should also be checking for concurrent modification here, just in case someone decides to Change the array for some reason why we're modifying it might add that later might not. Also, there's the next method, which just returns the next element in the array relative to the iterator. Okay, and lastly, use the to string to get a string representation of this array.

Nothing too complicated. All right. So this is a very simple data structure. It's just a dynamic array. If you look at Java's ArrayList, it looks very similar to this. So guys, thanks for watching and I will catch you in the next video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.