welcome to Amigoscode in this video I want to talk
to you why you should use Arrays instead of Lists
come on hold on don't start yelling at me and let me explain why so this is very important because
about a week ago I've done a poll asking people the number of experiences that they had whether
they were Junior mids senior developers and majority of them said that they were Juniors
and this is why I feel like it's important for me to address to all of you guys out there that you
should use race instead of Lists now if you take this example right here where we have a List in
here so if I want to add an element I can add an element like this just saying names.add if
I want to remove I just say remove in here and then pass the object if I want to know
the size I can just say names or size if I want to check whether it contains so basically I can
say names Dot and then contains there we go oops contains and then the object a right now in here
if you're starting out what are you learning you are learning nothing okay and let me
actually show you that in order for you to be a good software engineer so starting out from
your career you need to know how things work internally now let me show you something if we
check the List class so here I'm going to press command and then B let's go inside of the List
oh actually interface so this interface extends collection and let's just go back yet so we say
List and then new ArrayList so this is one implementation let's look into this
implementation ArrayLList right so ArrayList I'm going to press command and then navigate into it
and this takes me to this Constructor and have a look this element data now this is where the
data is actually being stored now if I click on this element data so command and then click
on it again have a look Transit and then object and what is this this is an Array
this is an Array so the ArrayList is backed by an Array internally so this is how it's able to
store values so on and so forth right now if you basically here say names.add right so let's just
look into names.add so the add method within the ArrayList so here I can go to structure and we
can look into the add method so add in here so add has multiple calls so basically increments
the count and then add let's just go into that and basically what it's doing here have a look so add
it checks whether s equals the element data and if so then it actually grows it expands and this is
because like once you define the size for the Array you are not longer able to increase the
size unless you grow the Array itself and this is what the add method is doing and then obviously
here then it's adding the element so have a look element data s equals to e so e is whatever you
pass into it and then is adding one to the size as well so if you're looking to grow so let's look
into grow so basically grow here this returns a brand new Array literally so let's go into grow
and you can see that basically grows the size plus one so that it can accommodate the new element
and inside of this grow method you can see that there's some logic inside but what I'm more
interested just to show you have a look element data equals to Array copy of in here or Arrays
copy of element data plus the new capacity so you can tell from this is that internally the
ArrayList is using an Array to store data when you say add remove contains there is a specific
implementation so here if you're using for example ArrayLists is going to have its own
implementation if you're using linked List you will have its own implementation because they
work differently internally and that's why we use the List interface and this is key this is
key so let's just go one more time let's just look into contains for example So within the
ArrayList if I open up the structure and let's look into contains oh it's right here contains
so here let me just put this full screen and basically it's just saying index of so this
is a method let's look into it again index of range let's look into it and check this out so
this is actually performing a loop right so it's going from start to the end and checking whether
the element is there right so there's some more stuff into it and then it's just returning one if
it doesn't find it otherwise it just Returns the index of the element that it finds now again you
can see that ArrayList uses an Array to store its data and when you call add remove contains and a
bunch of other methods there's specific logic that it's performed now the reason why I say
to people that basically join the Amigos code masterclass and in general people that I teach
learn how to use Arrays so don't start with Lists so get rid of all of this in here just like that
and now what you need to do is you need to learn for example how to add then also how to remove
contains grow right so basically if you want to add and if the List is full what do you need to
do so you need to grow and then basically copy the elements from the current Array into the
new one so on and so forth and with this you're actually learning how to use the data structures
that Java has to provide right so you're not just calling a method add and then there's magic behind
it and to be honest like it's more about you it's more about your journey okay and this is why with
our course so here we have the CLI project and there's this implementation right here
so danger you must use Arrays to hold any data do not use Lists at this point and this will be later
so hopefully now you see that if you are signing out you should use Arrays instead of Lists and
obviously like as you become more experienced and you know how things work internally
trust me you will use Lists all the time right List is the most used data structures within
Java more specific ArrayLists okay but it's important that you know exactly how to add how
to remove how to Loop perform like what I need you is to be able to write code to exercise to
practice and that way in your mind oh I want to add and then you start thinking about you know
some of the edge cases because if you don't then everything is just done for you and you
don't know internally how things work so this is pretty much it I would like you to comment down
below and let me know what you think about what I've just said okay should you start with a race
or simply Lists but as I said Lists is later and not at the early stages of you learning Java for
example okay so literally just take one second smash the like button subscribe if you haven't
done so let me know what other videos you want to see next and I'll see you in the next one