Array vs. ArrayList in Java Tutorial - What's The Difference?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
when you got started programming in java and began working with data structures you probably first learned about arrays and then a little bit later on you probably also learned about array lists but it's super easy to get them confused when are you supposed to use one versus the other what are the differences in how you create it add things to it remove things from it we're going to talk all about all of that in this video and then i'll tell you exactly which one i use and recommend that you use the vast majority of the time and why hi i'm john and if this is your first time watching this channel i put up a brand new java lesson video every single week it could be a lesson on a java concept or a full coding tutorial where i programmed something beginning to end so please be sure to like and hit the subscribe button so you don't miss the video every week and a sincere thanks to those of you who do take the time to like and subscribe it's the only way this channel grows so i really do appreciate it as always the full source with all the examples for this video is available in a link down in the description so go and get it so let's get right into it arrays and array lists so when would you use any of these at all well typically an array or an array list either one are used when you have just a group a collection of the same types of objects and you just want to hold a bunch of them you can have an array of just numbers maybe a list of cards in a deck or even look an array of string arguments to pass to your main method and if you've wondered what that's all about that string array args and the main method declaration in every single java program you've made ever be sure to check out this video i made all about that main method it describes exactly what that string array is used for and how you can use it in your own programs anyway let's say for your purposes you want to create an array of strings let's say you just have a list of your friends so you want to create an array that holds all of your friends names right so here's how you would do that if you wanted to use an array you would say string and then open close bracket that indicates that this is an array of strings so that's just java's type declaration and then after that of course you want to have your variable name just like you do for any variable so here since we're going to create an array of friends names let's just call it friends array and then you say equals and we need to initialize this array well there's a couple of ways you can do it the first way is just that you can say new string array but you have to also give it the capacity so notice that right now eclipse is giving us an error that says variable must provide either dimension expressions or an array initializer well basically what it is saying is you need to give this a size arrays in java have a fixed size which means that after you've declared your array it can't grow or shrink it has to stay that exact size so here we have to start out and declare its size so let's just say it we give it size four so this array can hold four elements inside of it and since that size is fixed for arrays that array will never ever grow or shrink from four it will always have exactly four elements some of those elements might be null and actually when a string array starts out like this all four elements will be null until we set them but it will never contain fewer or more than exactly four elements you can also initialize the values in your array right away when you declare it meaning you can fill up your entire array with values within your declaration so to do that you just do string array same thing friends array so we need a different name here so we'll just call it friends array 2 and then say equals and then in curly brackets curly braces you just put in all of the values you want to have in your array separated by commas so we've got strings here so let's just say we've got john yours truly i think i could be my own friend um chris eric and luke so notice that when we did it this way we didn't have to specify a capacity that's because we have an implied capacity by the values that we set so since we're setting exactly four values the capacity of this array will be exactly four and again because arrays can't grow or shrink it'll always have exactly four values okay so that's how you declare and initialize an array so how do you do it with an array list so to make your arraylist you'll say array list and in angle brackets you put in the data type that you want to hold in this arraylist so of course here we want it to be of strings so we say string and then we put in your variable name just like we did above and we'll call this one friends array list and then we say equals and now we need to instantiate this arraylist so uh to do that we do it like we do a lot of objects we say new array list and then we do something a little weird here we have what's called a diamond operator it's just the full set of open and closed angle brackets and then an open and close parentheses and then a semicolon so this open and close angle brackets is often called a diamond operator in older versions of java you had to specify the data type you wanted to hold in your array all over again here in this part then in later versions of java they said that's kind of silly you're already specifying it here java can just infer that you still mean that this is uh an arraylist of strings and so now you can just use this which is called a diamond operator so now we do have to add our imports for arraylist so i'm using uh eclipse so i can just say ctrl shift o and organize my import but if you're not using any special ide this is the import you will need java.util.arraylist so notice i didn't have to specify a size like i did for the arrays but here in the arraylist i didn't have to specify a size and the reason for that is arraylists do not have a fixed size that is one of the big benefits of arraylists over arrays arrays always have a fixed size that you cannot change but array lists will dynamically either expand or contract their size automatically by however many elements you are adding or removing from it so it's pretty handy that way so it can make programming with them a lot easier and you can just like we filled up the array with an explicit list of values here we can also do something similar with arraylist what we do is array list a string this part's all the same we give it a name friends array list 2 equals new array list open and close angle brackets and then in the parentheses here you'll type in arrays dot as list and then you'll pass into this arrays as list method just a a comma separated a list of the values you want to have in your arraylist but no curly braces here just the values separated by commas john chris eric luke okay i'm gonna go ahead and move some of this down to a new line so you can see all of it okay so instead of just saying new arraylist which doesn't fill up any values at all you could say new arraylist then you pass in arrays dot as list and then explicitly the list of all the values you want to have in there there are some other ways you can do it such as only having this arrays.aslist part but that actually gives you what's called an immutable list it gives you a list that you can't change so you couldn't ever add remove or change any values inside of it doing it this way with the new array list and then passing in the arrays.adslist call gives you a list that is mutable that you can mess around with and do whatever you want with so that's what we want for this purpose here so this is the first set of differences right between the arrays and the array lists just declaring them and initializing them is different and also we know now that arrays have a fixed size while an array list has a completely dynamic sizing and another difference between arrays and arraylist before we move on is what kind of data they can actually hold now an array can hold basically anything it can hold a primitive data type like an int a long a boolean and it can also hold objects like string or whatever cat or dog kind of object that you make in your own programs however an array list can only hold objects it cannot hold primitive so if you try to make it an arraylist of ins you're going to get an error that's one very small limitation of arraylists but you can get around it pretty easily by just using the wrapper class of whatever primitive data type you want to put in your arraylist so here instead of int you can just put in integer and it'll work exactly the same way so really not much of a downside to something you should know if you want to use an arraylist with a primitive data type like int just use its associated wrapper class so just integer for inst capital l for long capital b for boolean that kind of thing okay let's change this back to a string now that we've gone over the differences in the instantiations let's go ahead and just clean up our code a little bit so that we can have an array and array list that have some values that we can mess around with so next how do we get values from an array versus getting a value from an array list to get a value from an array you just have to specify the index of the value that you want indexes for arrays and array lists both work the same they both start at zero which means they look something like this so the first element will have index zero the second element will have index one and so on since we have four elements in both our array and our array list examples uh the indexes will look like this zero one two and three for the last element in each one so let's say we wanted to get the second element in each of these so how do we get the second element from our friends array all you have to do is say friends array and then in brackets you specify the index that you want since we want the second item it will actually have index one because it goes zero then one now this piece of code doesn't do much on its own so we might wanna do something like print out the value that we get so instead we can do system dot out dot print line and then friends array and in brackets the index that we want which is one all right so now we should get the element at index one which is the second element of our list which should be chris and it is so how do we do exactly the same thing with our friends arraylist well let's go ahead and copy this so instead of having the index in brackets like that you actually do a method call so you say friends array list dot get and you pass in the index that you want so of course here we want index one just like we did above so let's run our code again and we should get chris printed out twice and we do so to compare the two in this case they're pretty similar and one isn't really easier or harder than the other you just do them a little bit differently so next how do you get the size of each of these let's say we made an array we want to know how big it is well to get the length of an array you do this so we can print it out again system dot out dot print line friends array dot length notice that's just a field there's no parentheses in that like there would be for a method call and that's because it's just a field on that array let's go ahead and run that and see if it works we should get a length of four and we do now similarly for an arraylist system.out.printline friends arraylist dot not length but size and notice that it is a method call and not a fields we have an open and close parentheses here because this size is a method call again these two are very similar one is a method call and one is a field this returns how big the array is this returns how big the array list is so next how do we add an element to the end of an array or the end of an arraylist well here's how you do it with arrays you can't do that with arrays and why can't you do that well it's what we talked about before arrays have a fixed size and can't grow or shrink so our friends array will always be of size 4 because that's how many elements we put into it when we initialized it so we just can't do that with our arrays but that's where array lists come in to kind of save the day so we can do that easily with our array lists and to do that you just say friends array list dot add you call the method add and then you pass in the element that you want to add so i wanted to add a mitch it's that simple so and we can prove that that works by copying this get and then we can instead get the element at index four which should be the fifth and last element of our arraylist and that should give us mitch and it does and that's another point on the side of arraylist and it's for the same reason as before arrays always have a fixed length and it can't grow array lists can grow dynamically like we're doing here actually let's take a second to add some comments what we're doing here add an element get size get element create next how do we set a particular element let's say we want to change the element at index 0 to be something else so instead of john i want to make it carl well it's pretty easy to do in either case in the case of arrays you just say friends array and pass in in brackets the index that you want to set and just set it just like you would any other variable so friends array at 0 equals carl and so now if i get the element at index 0 from my friend's array it should be carl instead of john and yep there it is so for arraylist again it's very similar except you do it with a method call instead of specifying your index in brackets so you just say friends array list dot set and then you pass in your index so here i want to pass in the index 0 for it to be the first element in the list and then as the second argument you pass in what you want to set the value to so again here i can say carl we can print that out as well so we want to do friends array list dot get the element at zero and yes we have properly changed the first element of the list to be carl instead of john next what if we want to remove an element from our array or our array list well again for arrays sorry you just can't do it can't do this with arrays we have a fixed length in our array so i can't just take out this second element and leave it with an array of size 3 because i can't change the size of my array but with arraylist it is dead simple with just a method call friends array list dot remove and as you can see you can specify one of two things when you call this method you can either specify the index so i could pass in zero it will remove that very first element at index zero from our list or i can specify the object itself so let's say i wanted to remove this second element from my arraylist i can either pass in the number one because the index of the second value is one that will remove it or i can pass in the text chris i could pass in the exact element i want to remove and since we've been messing with indexes so much let's switch it up and do it this way and we can prove that we have removed it because now when we get the element at index 1 it should be eric because chris should be removed from the list and yes it did the value at index 1 in our arraylist is now eric gotta say that's another point for arraylist the flexibility is just great the last thing we want to compare is just printing say you wanted to print your array or print your arraylist how do we do it well let's try the first thing you would probably try just dot print line friends array and similarly we'll do system dot out dot print line friends array list let's just print both of those out and compare what they look like so you can see the last two lines we printed out here this is printing out our array and this is printing out our array list so just looking at those you'll probably get a good idea of what i personally like better what happens when you print out an array is java just basically gives you the memory address of that array maybe that's what you want but probably not you probably want to print out all the elements in the array right that's what would make sense and that's exactly what the arraylist implementation does and that's because the arraylist has implemented the tostring method and it has made it print out in this nice fancy way where it does and you know an open bracket it prints out the first element and then a comma a space and the second element and so on see this is just so clean you can print out your list super easily and know exactly what is in it and that's something you just don't get easily with arrays if you want to print out all the elements in an array you pretty much have to do a for loop and iterate over all elements in that array and print it out but with an arraylist it is already done for you so if after going through all of this you say man arraylists really rock i should use those i think you are absolutely right what arraylist really is is a wrapper around java's arrays that make them so much easier to work with it gives you automatic resizing easy adding easy removing and easy printing and that's not even to mention all the other things that you get because an arraylist is part of java's collections so you can see just a glimpse of that by saying okay i'm going to take my friend's arraylist what methods can i call on it oh man i can see if it contains an object i can call for each on it and loop through every element in it i can call a method to see if it's empty i can see what the last index is i can easily sort the list i can create a stream from it and do all kinds of cool things from that it really is awesome how much the arraylist class gives you to use so it'll probably come as no shock to you that arraylist is the one that i use and recommend that you use the vast majority of the time it just makes life so much easier and it makes your code so much easier to work with using arraylist over regular arrays using an arraylist does add a little bit of overhead you know when you're adding and removing values and stuff java has to do some work behind the scenes to make that happen but the vast majority of the time for most use cases i've ever worked with the difference in speed was just so negligible and it flat out didn't matter and the arraylist just being so much easier to work with as a programmer just far and away outweighs the little bit of performance increase you'll get from just using arrays so i just don't think it's worth it to use a raise unless you absolutely have to if you enjoyed this video or learned something please let me know with a like and if you'd like to see more videos like this every single week where we go over a new java lesson or tutorial or complete coding walkthrough please be sure to subscribe so you don't miss the video every week again a special thanks to you for liking and subscribing it's the only way these videos get out to more people so i really do appreciate it i wonder if chris will be offended that i chose to use his name as the example of a friend to remove from my list sorry chris
Info
Channel: Coding with John
Views: 10,628
Rating: 5 out of 5
Keywords: array vs arraylist, array vs arraylist java, arrays vs arraylists java, arraylist in java, arrays in java, arraylist java, arraylist java tutorial, arrays and arraylists java, arraylist of objects java, difference between arraylist and linkedlist in java, array, arraylist, java tutorial, codingwithjohn, coding with john, java beginner tutorial, java arrays, arraylist vs array java, arraylist vs linkedlist java
Id: NbYgm0r7u6o
Channel Id: undefined
Length: 17min 36sec (1056 seconds)
Published: Tue Feb 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.