1 00:00:00,330 --> 00:00:03,480 You need to learn the difference between a raise an array lists. 2 00:00:06,790 --> 00:00:13,600 Both can store many values at the same time, there is one difference, arrays are fixed, array lists 3 00:00:13,600 --> 00:00:14,520 are sizable. 4 00:00:15,220 --> 00:00:19,630 So far you've used arrays and in this lesson you're learned to use array lists. 5 00:00:22,890 --> 00:00:28,380 We can start setting up by following this path and the resources and open up the folder array lists. 6 00:00:38,330 --> 00:00:43,780 Kate, now an array is fixed in size, once you define the size of an array, you cannot change it. 7 00:00:43,940 --> 00:00:45,710 The number of elements is fixed. 8 00:00:46,430 --> 00:00:51,950 So inside the class you'll notice a group with three names and Katie just joined the group. 9 00:00:51,950 --> 00:00:53,570 So you need to add her name as well. 10 00:00:54,020 --> 00:00:59,840 But you cannot resize an array, so you need to set another variable equal to a brand new array of four 11 00:00:59,840 --> 00:01:00,350 elements. 12 00:01:00,350 --> 00:01:05,180 String new group is equal to a new string array that can store four elements. 13 00:01:06,100 --> 00:01:09,670 And then create a for loop that runs through the length of the first three. 14 00:01:16,280 --> 00:01:21,800 And inside the fur luper and up to each element in the new era, equal to values from the old one. 15 00:01:27,930 --> 00:01:30,540 And now finally, you can add Katie to the group. 16 00:01:34,650 --> 00:01:40,040 Well, that was so much work just by visualizing the runtime, which I'm going to do right now. 17 00:01:43,170 --> 00:01:47,670 You'll see that it's really crazy how much work needs to go into first, updating every element from 18 00:01:47,670 --> 00:01:48,360 the old array. 19 00:01:50,810 --> 00:01:57,260 And then only then can we add Katie to the group to add one element, we had to make a brand new array 20 00:01:57,260 --> 00:01:58,010 and start over. 21 00:01:58,400 --> 00:01:59,900 This is not efficient. 22 00:02:03,420 --> 00:02:07,770 And that is why we have the array list, the array list is sizable. 23 00:02:11,430 --> 00:02:18,620 The Israelis can add or remove elements at will, and it can only store objects, it cannot store primitives. 24 00:02:19,050 --> 00:02:22,470 So I hope you can now realize why we did the lesson on wrapper classes. 25 00:02:27,350 --> 00:02:33,080 To create an array list, you need to write the class type array lists, write what it can store inside 26 00:02:33,080 --> 00:02:37,340 of a generic, and generics can only accept class types. 27 00:02:37,760 --> 00:02:43,520 Generics cannot accept primitive types, i.e., if you want to have an array, a list of whole numbers, 28 00:02:43,670 --> 00:02:47,030 you would write array list integer, not array list int. 29 00:02:47,900 --> 00:02:52,100 And the third step is to make space for a new object of the area list class. 30 00:02:57,720 --> 00:03:05,430 Inside sizable arrays that Java Right array list autocomplete import the areolas class, this array 31 00:03:05,430 --> 00:03:11,580 list is going to store string objects, I'm going to call it names will set names equal to a new object 32 00:03:11,580 --> 00:03:14,760 of the Israelis class that can store elements of type string. 33 00:03:16,880 --> 00:03:22,340 Now, remember, the list is an object, so it has many methods that can add or remove elements. 34 00:03:28,920 --> 00:03:31,710 So we're going to use the ADD method to add three names. 35 00:03:38,820 --> 00:03:39,480 Joe. 36 00:03:44,100 --> 00:03:45,990 Jim and John. 37 00:03:49,350 --> 00:03:54,330 And now you can loop through each element in the array lists, we can use Intellisense to complete a 38 00:03:54,330 --> 00:03:58,950 loop and here we'll make sure the loop runs through the size of the aerialists. 39 00:03:59,670 --> 00:04:02,820 The size of an array list returns the number of elements. 40 00:04:03,060 --> 00:04:04,950 It's equivalent to the length of an array. 41 00:04:07,440 --> 00:04:09,900 Some generic names that size and let's have a look. 42 00:04:19,320 --> 00:04:24,900 As expected, the size is three, and so the loop is going to start from equal to zero until it reaches 43 00:04:24,900 --> 00:04:29,450 the last index and inside the loop, we need to get the element that each index. 44 00:04:29,460 --> 00:04:32,080 But how do you access values from an object? 45 00:04:32,520 --> 00:04:34,230 I hope you guessed it a getter. 46 00:04:34,830 --> 00:04:37,080 The aerialists class defines a getter. 47 00:04:37,080 --> 00:04:40,110 Get the returns and Alimentarius for an index. 48 00:04:41,610 --> 00:04:47,400 Since the terrorist public, we can call it from any array list object using DOT syntax, so inside 49 00:04:47,400 --> 00:04:52,950 the loop, the names array list can get for every element at the index I. 50 00:04:53,830 --> 00:04:55,120 And we'll print the result. 51 00:05:05,340 --> 00:05:06,510 Launching the debugger. 52 00:05:11,900 --> 00:05:15,740 The getter returns every element at the requested index. 53 00:05:17,270 --> 00:05:22,970 OK, and now let's just say Joe decided to change his name to Joe Fresh, hmmm, how do you update your 54 00:05:22,970 --> 00:05:24,110 values in an object? 55 00:05:24,530 --> 00:05:26,210 I hope you guessed it a Seder. 56 00:05:26,750 --> 00:05:31,630 The Israelis class defines a Seder set that updates an element that a certain index. 57 00:05:31,970 --> 00:05:36,680 And so, as you can see, it expects to parameters the index and the elements. 58 00:05:37,430 --> 00:05:42,280 And since the ceteris public, we can call it from any object of the Israelis class. 59 00:05:42,710 --> 00:05:45,830 So I'm going to set the element at Index zero to Joe Fresh. 60 00:06:05,300 --> 00:06:09,530 And that's perfect to the setar update's the value, isn't it awesome? 61 00:06:09,740 --> 00:06:13,320 Java classes also use the big three steps the area. 62 00:06:13,340 --> 00:06:17,660 This class has constructor's from which we can create a list of objects. 63 00:06:18,050 --> 00:06:19,960 The area this class defines a getter. 64 00:06:20,210 --> 00:06:26,990 And since the letter is public, every aerialists object can call it using dot syntax and the Israelis 65 00:06:27,000 --> 00:06:29,030 defines a setar once again. 66 00:06:29,030 --> 00:06:34,160 Since the sitarist public, every aerialists object can call it using DOT syntax. 67 00:06:35,780 --> 00:06:41,870 In the aerialist class also defines other methods like add and remove, the first method adds an element, 68 00:06:41,870 --> 00:06:46,550 Alacer, an index, and the second method removes an element, Alacer, an index. 69 00:06:46,790 --> 00:06:51,440 And since these are public, we can call them from any object of the Israelis class. 70 00:06:54,240 --> 00:06:57,420 So I'm going to add an element at index three of the names object. 71 00:06:59,900 --> 00:07:00,380 Katie. 72 00:07:11,490 --> 00:07:14,160 And as expected, it adds an element that index three. 73 00:07:16,460 --> 00:07:20,990 We can also remove elements in this case, I'm going to remove the element and index to. 74 00:07:30,370 --> 00:07:33,880 And unsurprisingly, it removes the element of the next to. 75 00:07:35,160 --> 00:07:39,060 You know, the Aranesp class also defines a method that clears every element. 76 00:07:40,200 --> 00:07:44,370 And since the method is public, we can call it so I'm going to call it from the names object. 77 00:07:55,070 --> 00:07:57,050 And it clears every element. 78 00:08:03,180 --> 00:08:06,240 If you don't expect the size to change userspace. 79 00:08:08,850 --> 00:08:15,480 User Ray lists when the number of elements can vary, if you need to add or remove elements, then it's 80 00:08:15,480 --> 00:08:18,030 a thousand times easier to use an array list. 81 00:08:20,710 --> 00:08:25,750 And this lesson you learn, the difference between a raise, an array lists, arrays are fixed. 82 00:08:27,090 --> 00:08:29,430 Whereas array lists are sizable. 83 00:08:30,580 --> 00:08:37,750 If you don't expect the size to change, use a race, if you expect the size to change user realist's.