Java: Array vs ArrayList Difference

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I'm Joe James today we're going to look at the difference between arrays and array lists in Java the biggest difference between an array and an array list is that array has a fixed length it must be specified when it is created and can never be changed after is created the array list has a variable length that changes dynamically it automatically resizes itself to accommodate the amount of data you have you don't even need to specify the size of an ArrayList when you create it because it can use a default size that will adjust as the amount of the data changes so this difference is the biggest design consideration when deciding which one to use so if you have a fixed and known length of data then use an array and if the amount of data is either variable or often changing or unknown use an ArrayList arrays can support primitive data types like int floats and boolean z' and can also support objects but arrays cannot support generic data types so the data type has to be hard-coded where the array is created array lists can support objects and can support generic data types but they cannot support primitive data types now thanks to Java is auto boxing feature most primitive types can be used interchangeably with the corresponding object types so like int an integer so support for primitives is rarely a design consideration but support for generic data types can be so the next big difference ring.look has performance and since an ArrayList uses an array as its underlying data structure the performance of arrays and ArrayList is almost identical they're both extremely fast and they can both insert and retrieve data in constant time Big O of 1 so performance is rarely a factor in deciding which one to use but that said if you have a lot of data in your application and super performance in 10 then arrays are slightly faster now in ArrayList the resize operation which happens behind the scenes you don't really see that or another that's happening it will decrease performance very slightly because it involves copying all the data from the old array to the new one when it outgrows an old array but when amortized across all operations resize is really not a performance factor in average insertion time is still Big O of 1 now we're going to take a look at the implementation how the code differs for arrays and array lists so for create to create an array let's say we want to create an array of int we use the square brackets we name the array we'll call this one my array equals new int array and you must put the size the number of elements inside the square brackets so this will create an int array of size 10 now this line will also create an int array of size 10 but it also will initialize the values to 1 through 10 so you can actually assign using the curly braces if you're using primitive data types you can use the curly braces to actually assign values to the array when you initialize it and then if you want a data type let's say object type so let's say dog so we have dog's array is an array of dog so you would have to say equals new dog and then the number of elements the number of dogs you want to be in this array 25 that's how you declare and initialize an array let's look at an array list so with an array list since it supports abstract data types you have to include the data type in these angle brackets and here we use integer because ArrayList doesn't support in right that's a primitive data type but it does support integer my list equals new array list and then you can also put integer between these brackets after the equal sign if you want but you don't after and then like I said optionally you can include the size the default size will be used if you don't specify size so a ray list of cat called cats equals new ArrayList data type cat and here we just chose to use the default size again and and this is a similar declaration for cat except that we initialized it to 25 elements again since the array list will grow or shrink to the amount of data you have if you have a rough idea of how many elements you're going to be putting in it initially you can initialize the array list that size but if you have no idea then you can just use the default value so insert operations array actually doesn't insert it actually assigns so if you want to assign something to index 3 you could say my array square brackets 3 equals 7 just assign 7 to index 3 of my array array list actually uses the add function my list add 7 would add a 7 to the tail end of the array get operations my array square brackets 3 would return the item at index 3 so it's pretty simple with arrays with array lists it's also pretty simple but again the syntax is slightly different so my list get there's actually a get function that gets the item at index 3 and returns it and length my array length returns the length of my array and my list dot size again this is a you can see it's a method because it has parentheses here returns the length of my list so that concludes our video on arrays versus array lists I hope this video was helpful for you I'm Joe James thank you for watching you
Info
Channel: Joe James
Views: 109,706
Rating: undefined out of 5
Keywords: Array Data Type, Java (Programming Language), Array Data Structure, arraylist, array, Dynamic Array, performance, advantage, data structure, versus, comparison, differences, explanation, methods, functions, initialization, java
Id: ZVJ7kpEMc7U
Channel Id: undefined
Length: 6min 34sec (394 seconds)
Published: Fri Jan 16 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.