1 00:00:01,410 --> 00:00:02,040 Welcome back. 2 00:00:02,250 --> 00:00:07,080 And this video, we're going to look at ArrayList and we're going to use the rails quite a bit in our 3 00:00:07,590 --> 00:00:13,200 future project, so it's important to see how they are used and understand them so they're used to create 4 00:00:13,200 --> 00:00:17,290 dynamic arrays and arrays are types of collections. 5 00:00:17,310 --> 00:00:19,290 So we're going to use collections here. 6 00:00:19,950 --> 00:00:21,390 So why should you use an array lists? 7 00:00:21,780 --> 00:00:27,990 Well, relics are used to create a dynamic array, which means the size of an array list can be increased 8 00:00:27,990 --> 00:00:30,180 or decreased according to your requirements. 9 00:00:30,480 --> 00:00:33,510 So it's not limited to a specific size, which is pretty cool. 10 00:00:34,020 --> 00:00:37,560 The ArrayList class provides both read and write functionality. 11 00:00:38,580 --> 00:00:45,000 Then the ArrayList follows the sequence of insertion order, so it matters when you put something in 12 00:00:45,000 --> 00:00:50,460 there and then the radius is non synchronized and it may contain duplicate elements. 13 00:00:50,760 --> 00:00:56,820 So it's not going to be limited to one entry of the same thing, so to speak. 14 00:00:58,170 --> 00:01:00,180 The constructor of an array list looks like this. 15 00:01:00,360 --> 00:01:06,060 So either this used to create an empty ArrayList, so that's the basic constructor. 16 00:01:06,070 --> 00:01:12,240 Then you have another constructor where you can actually pass in the capacity so you can see what the 17 00:01:13,470 --> 00:01:17,640 maximum amount of elements that can fit into this array test should be. 18 00:01:18,120 --> 00:01:23,850 And then you can create and the rate is based on a collection so you can fill it with the elements of 19 00:01:23,850 --> 00:01:27,420 a collection and use it then as you need it. 20 00:01:28,230 --> 00:01:34,050 Examples of functions of a rapist are, for example, the add function used to add the specific element 21 00:01:34,050 --> 00:01:34,980 into the collection. 22 00:01:35,790 --> 00:01:39,930 Then the clear function is used to remove all elements from a collection. 23 00:01:40,380 --> 00:01:45,420 To get function allows you to get an element as the specified index. 24 00:01:45,450 --> 00:01:51,030 So, for example, if you want to have the fifth entry, you could enter index for in there and you 25 00:01:51,030 --> 00:01:51,570 would get that. 26 00:01:52,260 --> 00:01:59,670 And then the remove function is used to remove a single instance of the specific element from the current 27 00:01:59,670 --> 00:02:01,650 collection if it's available. 28 00:02:01,980 --> 00:02:08,190 And hereby I say a single instance, so let's say you have to value five multiple times in there. 29 00:02:08,490 --> 00:02:14,010 Only the first five would be removed if you call this remove function with the value of five. 30 00:02:15,190 --> 00:02:20,110 Then there are, of course, many more functions in the ArrayList class, and we're going to use multiple 31 00:02:20,110 --> 00:02:23,170 of them, but these are the most important ones. 32 00:02:23,800 --> 00:02:28,960 All right, let's have a look at an actual ArrayList so you can see we create an array list and we created 33 00:02:28,970 --> 00:02:30,850 via the valid keyword. 34 00:02:30,860 --> 00:02:37,600 So we're not using VAR here because even though the ArrayList might be a vowel, so an immutable ArrayList, 35 00:02:37,930 --> 00:02:39,340 it's still adjustable. 36 00:02:39,550 --> 00:02:46,480 So we cannot replace the ArrayList object, but we can replace the content of this ArrayList object. 37 00:02:46,810 --> 00:02:51,250 In this case, it's in ArrayList object of type string, so we only can add strings in there. 38 00:02:51,520 --> 00:02:55,060 For example, the ArrayList add one and then ArrayList add two. 39 00:02:55,390 --> 00:03:01,900 So I'm adding one and two to this ArrayList, meaning the written words. 40 00:03:02,170 --> 00:03:07,630 And then we can print this ArrayList by using the for in loop here. 41 00:03:07,640 --> 00:03:16,730 So for I in ArrayList, we print I and that will output one hand to the rest using collections. 42 00:03:16,750 --> 00:03:17,730 Let's have a look at that. 43 00:03:17,740 --> 00:03:22,330 We can see we have again the function main and then we create an array list this time. 44 00:03:22,720 --> 00:03:27,040 We set this list as being limited to five entries. 45 00:03:27,040 --> 00:03:34,260 So this is the one example where you can limit an array list to add a certain amount of values. 46 00:03:34,330 --> 00:03:42,580 So here this one, and then we have a list, which is a mutable list of strings and we put values in 47 00:03:42,580 --> 00:03:46,810 there and then we add all of those values to our ArrayList. 48 00:03:48,630 --> 00:03:57,810 Then we use an ArrayList iterator called Ittihad in our case, and we say as long as this iterator has 49 00:03:57,810 --> 00:04:03,630 something so as long as our ArrayList has another entry in there, we run to run this while loop. 50 00:04:04,050 --> 00:04:06,930 So we're just printing it our next. 51 00:04:07,140 --> 00:04:14,940 So the next entry in our ETR, which in this case means in the ArrayList iterator, which pretty much 52 00:04:14,940 --> 00:04:16,380 means in the ArrayList itself. 53 00:04:16,860 --> 00:04:22,860 And then we can also see that we can get the size of an ArrayList by using its size property. 54 00:04:23,160 --> 00:04:31,260 So that will give us the following output one two and then, of course, the size of a realist as to 55 00:04:31,260 --> 00:04:33,450 because there are only two entries in there. 56 00:04:37,920 --> 00:04:43,180 Now, let's look at the get function, so you can see we have this main function here again with. 57 00:04:44,130 --> 00:04:49,140 So then let's look at the get function you can see again, we have a main function here using an array 58 00:04:49,140 --> 00:04:54,270 list of strings and then we add one and two and then we loop through it with this for loop. 59 00:04:54,570 --> 00:05:00,010 But we also used to get function here in order to see what we will get. 60 00:05:00,060 --> 00:05:04,050 So if I use get one, I will get two. 61 00:05:04,290 --> 00:05:08,880 So this is a little confusing, but that has to do with the counting starting at zero. 62 00:05:08,890 --> 00:05:12,300 So the first entry here is going to be at index zero. 63 00:05:12,330 --> 00:05:14,400 The second entry will be at index one. 64 00:05:14,700 --> 00:05:22,530 And what we're doing here is we are accessing index one, which means we get the two, and that's why 65 00:05:22,530 --> 00:05:24,270 the output is two in this case. 66 00:05:26,310 --> 00:05:29,910 All right, this was just a little introduction to ArrayList. 67 00:05:30,210 --> 00:05:33,900 We are going to use them quite a bit and then they will become natural as usual. 68 00:05:34,140 --> 00:05:35,370 So no worries there. 69 00:05:35,640 --> 00:05:39,030 It's like learning a new word in a foreign language. 70 00:05:39,450 --> 00:05:45,150 The more context you see this word and the more it makes sense and the better of an understanding you 71 00:05:45,150 --> 00:05:45,870 get for this word. 72 00:05:46,530 --> 00:05:47,820 So see you in the next video.