You need to learn the difference between a raise an array lists.
Both can store many values at the same time, there is one difference, arrays are fixed, array lists
are sizable.
So far you've used arrays and in this lesson you're learned to use array lists.
We can start setting up by following this path and the resources and open up the folder array lists.
Kate, now an array is fixed in size, once you define the size of an array, you cannot change it.
The number of elements is fixed.
So inside the class you'll notice a group with three names and Katie just joined the group.
So you need to add her name as well.
But you cannot resize an array, so you need to set another variable equal to a brand new array of four
elements.
String new group is equal to a new string array that can store four elements.
And then create a for loop that runs through the length of the first three.
And inside the fur luper and up to each element in the new era, equal to values from the old one.
And now finally, you can add Katie to the group.
Well, that was so much work just by visualizing the runtime, which I'm going to do right now.
You'll see that it's really crazy how much work needs to go into first, updating every element from
the old array.
And then only then can we add Katie to the group to add one element, we had to make a brand new array
and start over.
This is not efficient.
And that is why we have the array list, the array list is sizable.
The Israelis can add or remove elements at will, and it can only store objects, it cannot store primitives.
So I hope you can now realize why we did the lesson on wrapper classes.
To create an array list, you need to write the class type array lists, write what it can store inside
of a generic, and generics can only accept class types.
Generics cannot accept primitive types, i.e., if you want to have an array, a list of whole numbers,
you would write array list integer, not array list int.
And the third step is to make space for a new object of the area list class.
Inside sizable arrays that Java Right array list autocomplete import the areolas class, this array
list is going to store string objects, I'm going to call it names will set names equal to a new object
of the Israelis class that can store elements of type string.
Now, remember, the list is an object, so it has many methods that can add or remove elements.
So we're going to use the ADD method to add three names.
Joe.
Jim and John.
And now you can loop through each element in the array lists, we can use Intellisense to complete a
loop and here we'll make sure the loop runs through the size of the aerialists.
The size of an array list returns the number of elements.
It's equivalent to the length of an array.
Some generic names that size and let's have a look.
As expected, the size is three, and so the loop is going to start from equal to zero until it reaches
the last index and inside the loop, we need to get the element that each index.
But how do you access values from an object?
I hope you guessed it a getter.
The aerialists class defines a getter.
Get the returns and Alimentarius for an index.
Since the terrorist public, we can call it from any array list object using DOT syntax, so inside
the loop, the names array list can get for every element at the index I.
And we'll print the result.
Launching the debugger.
The getter returns every element at the requested index.
OK, and now let's just say Joe decided to change his name to Joe Fresh, hmmm, how do you update your
values in an object?
I hope you guessed it a Seder.
The Israelis class defines a Seder set that updates an element that a certain index.
And so, as you can see, it expects to parameters the index and the elements.
And since the ceteris public, we can call it from any object of the Israelis class.
So I'm going to set the element at Index zero to Joe Fresh.
And that's perfect to the setar update's the value, isn't it awesome?
Java classes also use the big three steps the area.
This class has constructor's from which we can create a list of objects.
The area this class defines a getter.
And since the letter is public, every aerialists object can call it using dot syntax and the Israelis
defines a setar once again.
Since the sitarist public, every aerialists object can call it using DOT syntax.
In the aerialist class also defines other methods like add and remove, the first method adds an element,
Alacer, an index, and the second method removes an element, Alacer, an index.
And since these are public, we can call them from any object of the Israelis class.
So I'm going to add an element at index three of the names object.
Katie.
And as expected, it adds an element that index three.
We can also remove elements in this case, I'm going to remove the element and index to.
And unsurprisingly, it removes the element of the next to.
You know, the Aranesp class also defines a method that clears every element.
And since the method is public, we can call it so I'm going to call it from the names object.
And it clears every element.
If you don't expect the size to change userspace.
User Ray lists when the number of elements can vary, if you need to add or remove elements, then it's
a thousand times easier to use an array list.
And this lesson you learn, the difference between a raise, an array lists, arrays are fixed.
Whereas array lists are sizable.
If you don't expect the size to change, use a race, if you expect the size to change user realist's.