(upbeat music)
- An array is of collection of objects
of the same data type, and its size,
cannot be changed after it's allocated
since it's items are stored
at contiguous locations and memory.
each object
in the array is accessed by index starting at zero.
And there are specific ways to declare, allocate
initialize, add, and remove objects to an array.
Now, what are some advantages of arrays?
While arrays can be used to implement other data structures
and can represent multiple data items of the same type,
by using only a single name.
They also have
in dimensions and less overhead than other data structures.
So if you have a list that will not change
in size arrays are the way to go.
let's take a look at some code first here on line 19
we declare an array. Here it's an array, that's an interray
and we give it the name Nums, next online 22
we declare an allocate in array.
This is a double array called other nums
and it has five slots in its array.
We're allocating space, five spaces for this array.
Now for the majority of the demo,
we are gonna pretend we own a pet shop. In our pet shop
We have pets that are currently available
and others that are currently unavailable
to represent this we've initialized two string arrays
with available and unavailable pets in our pet store.
In this case, cat, dog
and fish are available and bird, rabbit,
hamster and gerbil are unavailable.
With these arrays we're declaring, allocating
and initializing them with these created.
We can print out these arrays using the arrays
dot two string class method.
So we'll go system dot out dot println arrays dot toString
and then put in available pets.
And then do the same thing here
with our unavailable pets running the program
we'll be able to see these arrays in the console.
And so we have cat dog fish
and available pets and bird, rabbit, hamster
and gerbil in our unavailable pets.
Now let's say we wanted to move one pet from available
to unavailable in another pet from unavailable to available.
This will require retrieving, removing and adding items
to both the available and unavailable pet arrays
and essentially swap one animal for the other.
To do this, let's determine which objects
at what indices we wanna swap.
And so in this case, we wanna swap fish with bird.
And so the first step to doing that
is we'll go int index of available pets.
And basically what we're gonna do here
is get the index of both fish and bird
the objects we wanna swap.
So we'll get the index at the available pet
which is fish, and this is two.
And then we'll get the index of the unavailable pet,
which is zero bird.
Next will achieve the current available and
unavailable pets that we wanna swap the actual strings
the actual objects, and we'll put'em into some variables.
So we'll go string available pets equals available pets
and then use our index that we just got from before.
And then we'll have our unavailable pet
and use that same index from before.
And so now we have our fish
and bird variables to add the bird
to the available pets array and delete the fish,
we'll simply access the available pets array.
So we'll go available pets, and then we'll use the index
of the current available pet fish.
And so we'll use index of available pets.
And in that slot where fish is,
we're gonna wanna put our bird.
And so that's where we're gonna put unavailable pet.
Next to add the fish to the unavailable pets array
and delete the bird we'll access unavailable pets
using the index of the current unavailable pet, bird.
And we'll wanna put our fish there.
And so we'll go available pet let's go ahead
and print these out to see that the swap happened.
We can actually just copy the lines that we created up here
before to see the current arrays surrounding the program.
We see that bird is in the fish's old spot.
So we scroll up, we see bird here instead of fish.
And then in the second array, we see fish instead of bird.
And so bird and fish were effectively swapped.
Now what happens if you wanna add something
to an array that's full without deleting anything?
Ultimately you have to copy everything
to a new bigger array and then add your item.
But this can be time consuming.
You can also make a huge array
but then you're wasting a bunch of space.
This makes insertion and deletion
much more difficult and time consuming.
Now, what if you wanna search
for something that's in an array?
We have this method up here
at the top of our class online sex.
And basically what it does is it finds
if a certain item is in an array.
And if it is returns true, if not returns false
ultimately to find if something's in an array
you have to iterate through the entire array
using a loop to find the item you're looking for.
Even if it's not there'
(Upbeat music)