Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video we will discuss the difference between arrays and ArrayBuffers, as well as go through an example of how to create, append, and remove them.
New Terms:
Arrays -- An array is a data structure which stores a collection of elements of the same type. Arrays have a fixed size.
ArrayBuffer -- An ArrayBuffer is very similar to an array, except it can grow in size after it has been initialized.
Further Reading:
Arrays
ArrayBuffers
Array documentation & methods
ArrayBuffer documentation & methods
An array is a data structure which
stores a collection of elements and
0:00
has a fixed sized.
0:04
An array buffer on the other hand,
gives us the same features as an array but
0:05
provides flexibility in terms of size.
0:09
The array buffer can grow and
shrink as we need.
0:13
Let’s dive into the code and
get comfortable with both.
0:16
Lets fire up IntelliJ and create a new
package within our Source directory.
0:20
Right-click on Source,
select New > Package.
0:25
Provide a package name,
in our case collections.
0:29
And then we'll copy our
SuperHeroes object.
0:32
And paste it into our collections package,
click OK.
0:39
And within our SuperHeroes object
we can get rid of everything but
0:42
our main function.
0:46
We will use this object as our main
class to run all of our operations.
0:53
But in order to do so we will also
need to edit our configurations.
0:57
So we'll click Edit Configurations,
and for our main class,
1:01
we'll select the SuperHeroes
in the collections package.
1:05
Click OK, and then Apply, and OK.
1:10
There are a few different
ways to create an array.
1:13
Let's dive into how arrays
work by implementing them.
1:16
Great, we create the array by supplying
the new keyword, followed by array,
1:27
the type, and also the array size.
1:32
Here we've created an array of integers
called numbers with length 10,
1:34
all initialized to the integer 0.
1:38
Similarly, we can create
an array of all strings
1:41
Again, we supply the new keyword,
the array keyword,
1:52
the type information, and the array size.
1:56
Often times we may want to create
an array with some initial values.
1:59
In that instance we don't need
to provide the new key word.
2:03
Let's take a look.
2:06
You may have noticed that we don't have
to specify the type that's because
2:21
Scala's type inference can in further type
from the values we passed to the array.
2:25
A big different from Java and
many other languages,
2:30
which you may have used to access values
at a specific index of the array,
2:33
we use parenthesis instead
of square brackets.
2:38
Arrays are 0 index and we can access the
fourth element by using the parenthesis.
2:45
In this case this should print out Batman.
2:50
Let's compile and
run our application, awesome.
2:53
One issue that we will encounter is adding
or removing elements from our heros array.
2:58
Let's say we wanted to add
Iron Man to our list of heroes.
3:03
We can use the colon plus
method on our arrays.
3:06
However, this would create
an entirely new array.
3:10
Array buffers to the rescue.
3:13
To create an array buffer we need to
import the scala.collection.muteable
3:15
package or specifically the array
buffer class within the package.
3:19
Let's do so at the top of our file and
create a new array buffer.
3:24
[SOUND]
3:34
Awesome.
3:57
To add a new element to the ArrayBuffer,
we would use the += method.
3:57
Additionally, we can add multiple
elements by enclosing them in parentheses
4:07
Similarly, we can add an entire collection
by using the plus plus equals method.
4:11
Let's create an array to
append to our mutable heroes.
4:17
Awesome, here append other
heroes to our mutable heroes.
4:44
Arrays and array buffers have a ton built
in functions you should get familiar with.
4:48
I've included links in the teacher
notes to the actual documentation.
4:52
Remember our array buffer is mutable.
4:56
So let's run a few operations on
our heros to get more familiar.
4:59
Let's print out the contents
of our array buffers so
5:04
we have an idea of what
it's currently storing.
5:06
Awesome.
We see that we have an array buffer
5:15
with all the values
that we've added to it.
5:17
Additionally we can remove a number
of elements starting from the right
5:19
of the array buffer.
5:23
TrimEnd allows us to remove a number
of elements starting from the end of
5:30
the array buffer.
5:34
In this case we make use of the update
method which lets us update the value at
5:45
a specific index.
5:50
Therefore replacing Thor with Flash.
5:51
Let's reprint the contents of
our mutableHeroes array and
5:55
see how things have changed.
5:58
Rerun our application.
6:03
And the first thing we notice is that
Thor has been replaced with Flash.
6:05
Additionally, we notice that Starfire,
as well as Green Arrow, have been removed
6:10
from our array buffer Awesome we learned
how to create arrays and array buffers.
6:15
Now let's traverse through our
mutable heroes array buffer.
6:20
As we previously covered we can
use the four loops in Scala
6:24
to loop through an array or
an array buffer.
6:27
We'll comment out our previous
print line statements.
6:41
Here, we follow the left arrow operator,
6:47
which loops through each
element in the array buffer.
6:49
Let's recompile and rerun our app.
6:52
Great, now we've printed out
every element in our mutable
6:57
heros array buffer to the screen.
7:00
If you recall from an earlier lesson,
Scalas supports inclusive and
7:03
exclusive ranges.
7:07
For an exclusive range,
we use the keyword until.
7:08
Let's recompile and run our app.
7:19
Awesome.
7:24
We notice that our result,
10, is not included.
7:24
For an inclusive range,
we have to use the keyword to
7:28
Let's recompile and run our app.
7:39
Great and
in this case we see that 10 is included.
7:44
However there may be certain instances
where we'd also like the index values.
7:47
In this case we print out the index and
the value of the specified index.
8:12
Until provides us with an exclusive range,
so
8:17
there's no need to subtract one
from the array buffer length.
8:20
Awesome, as expected we get the index
value along with the element
8:23
at that index.
8:27
With an inclusive to,
8:28
we will need to subtract one
from our mutableHeroes length.
8:30
Otherwise, we would get
an index out of bounds error.
8:33
In earlier videos we learned about
four comprehension and guards.
8:56
These concepts are very useful
when transforming arrays.
9:00
Let's create a new array with any
heros whose name contains a dash.
9:04
The yield keyword will create the same
type of collection as the one we start
9:24
with and the guard will provide filters
on our data to any element in the array
9:29
that contains a dash.
9:33
Let's see how that works
by printing out the element
9:35
in the dash heroes array buffer.
9:38
As expected, our dash here as array
buffer only contains one element,
9:44
Spider-man, as it's the only element
that has a dash in its name.
9:48
Great, we learned how to create, traverse,
and update arrays and array buffers In
9:53
the next video we'll focus on another
set of data structures called list and
9:57
list buffers.
10:02
See you there.
10:02
You need to sign up for Treehouse in order to download course files.
Sign up