declare

ArrayList in Java Tutorial



Sharing buttons:

you may have heard of a rayless and it's

like we have a raise why why do we need

something else but in this video I'm

gonna show you exactly what arraylists

are how they differ from a raise and how

you can use them to make your life

easier

what is up my name's Alex welcome back

again to another Java video on this

channel I hope you learn java by posting

a new java tutorial every week so if

you're new here consider subscribing

let's get this party started by going to

file new Java project we'll call it

something like arraylists project finish

open it up click on source go to new

class and we'll call it array lists

project again hit this first check mark

and hit finish so I'll just go over

arrays again say we want to have an

array of fruits which are strings this

is how you make an array and we need to

know the amount of items in it to add an

element into our fruit so you want to

add a mango mango now we'll add the rest

if we print these to the screen will say

fruits 1 save it and run it we'll see

the apple now here's how you do this

exact same thing into an array list

instead of an array we would type array

list and we'll name it a fruit list and

finish it off by typing this code right

here hover over array list this lets

Java know hey we want to import

ArrayList into a program that just means

we're gonna be using it add fruits into

our fruit list we just typed the name

fruit list and then do a dot this brings

up all the things that this ArrayList

can do you don't need to know all of

them but today we're gonna be looking at

this add 1 so click add hey we'll put

our mango in there finish it off by

adding our other two fruits fruit list

add Apple

fruit list dot add oops

strawberry no print out all of the

fruits by doing systems that out the

print line fruitless like that will save

it and run it we see the Apple from the

fruits array and then we see all three

elements from the ArrayList if we tried

to print out elements from the array

like this fruits and tried that it would

print out this gibberish so the

ArrayList is more flexible because we

can see all the elements by printing

them out but arrays are a little more

work here's another difference if you

want to add another fruit let's say

let's add a watermelon to an ArrayList

we do fruitless dot add watermelon save

and run that it's automatically added

that was super easy but for an array a

regular array we would have to create a

third index and put watermelon in here

save it let's see what happens we get an

error because we actually have to change

the size so with an array you have to

know the exact size every time but array

lists are flexible they don't care they

can grow and shrink whenever you want

this is useful for things like YouTube

subscriptions people are subscribing and

unsubscribing to channels all the time

so that list grows and shrinks every day

it would be super annoying to have to

recreate your array so they just use an

ArrayList probably or something like an

ArrayList some other neat things you can

do I'm just gonna save and run this

again no errors this time we can remove

elements from an array list by typing it

doing a dot and then scrolling down to

the one that says remove and click

remove say we want to remove strawberry

we can do that really easily save and

run that no strawberries gone you can

remove all the elements from an

ArrayList by doing fruitless clear

save and run and now it's empty you can

really do all sorts of things with it

I'm just gonna try one more thing we'll

see oh okay we can see if our free list

contains something so we'll see if like

there's a raspberry in there which

there's not and we'll print out the

results of that because this will give

us true or false I'll comment this out

for now since we don't want to clear it

save and run see what happens false it's

because our fruit list does not contain

raspberry so none of this cool

functionality is available to you with a

race you would have to code it yourself

so we use array lists because they're

more flexible you can add remove clear

see if it contains what you're looking

for and it raised a really bare bone

they're better for smaller things if you

know their exact size likes on videos

and comments always make me happy

because it means I'm helping you and

these videos are helpful for you but for

now I'll talk to you later you could be

anywhere in the world but you're here

with me and I appreciate that so much

thanks see you next week bye

[Music]