1 00:00:00,132 --> 00:00:01,590 ‫Narrator: Hi. 2 00:00:01,590 --> 00:00:02,896 ‫Inside of this lecture we're going 3 00:00:02,896 --> 00:00:05,865 ‫to learn something called array list. 4 00:00:05,865 --> 00:00:09,030 ‫Working with arrays is cool actually. 5 00:00:09,030 --> 00:00:11,940 ‫Okay, I'm going to name these arrays 6 00:00:11,940 --> 00:00:16,410 ‫and how even cooler data structure called 7 00:00:16,410 --> 00:00:18,210 ‫the array lists. 8 00:00:18,210 --> 00:00:20,460 ‫So sometimes you see as lists 9 00:00:20,460 --> 00:00:22,140 ‫in other programming languages. 10 00:00:22,140 --> 00:00:24,210 ‫In Java, we call them array lists 11 00:00:24,210 --> 00:00:25,830 ‫but they're basically the same. 12 00:00:25,830 --> 00:00:27,510 ‫It's actually the same thing with arrays 13 00:00:27,510 --> 00:00:29,100 ‫but it's much more flexible 14 00:00:29,100 --> 00:00:31,470 ‫when we compare it to the arrays, 15 00:00:31,470 --> 00:00:33,720 ‫because over here when you initialize 16 00:00:33,720 --> 00:00:35,430 ‫a new empty array 17 00:00:35,430 --> 00:00:38,700 ‫you have to specify some kind of perimeter 18 00:00:38,700 --> 00:00:43,200 ‫that limits the capacity of those arrays. 19 00:00:43,200 --> 00:00:45,420 ‫But inside of an array list, 20 00:00:45,420 --> 00:00:47,850 ‫you don't have to do that at all. 21 00:00:47,850 --> 00:00:50,100 ‫You can just initialize an array list 22 00:00:50,100 --> 00:00:52,305 ‫as an empty list and you can add 23 00:00:52,305 --> 00:00:55,110 ‫or just remove any element 24 00:00:55,110 --> 00:00:58,110 ‫that you may want later on. 25 00:00:58,110 --> 00:01:00,720 ‫For example, you can just write array list 26 00:01:00,720 --> 00:01:03,030 ‫and beware, there is some kind of 27 00:01:03,030 --> 00:01:04,712 ‫angler parenthesis over here 28 00:01:04,712 --> 00:01:08,404 ‫which is smaller or bigger size, 29 00:01:08,404 --> 00:01:12,270 ‫and over here you have to specify the type 30 00:01:12,270 --> 00:01:16,050 ‫of the variable that you're going to be storing 31 00:01:16,050 --> 00:01:18,030 ‫and those array lists. 32 00:01:18,030 --> 00:01:20,340 ‫So I'm going to call string for example, 33 00:01:20,340 --> 00:01:22,373 ‫because they will be a string. 34 00:01:22,373 --> 00:01:25,710 ‫It's going to be a string array list. 35 00:01:25,710 --> 00:01:28,380 ‫I'm going to call this my musicians, 36 00:01:28,380 --> 00:01:29,213 ‫okay? 37 00:01:29,213 --> 00:01:31,324 ‫And I can come over here and just say 38 00:01:31,324 --> 00:01:33,750 ‫this is going to be a new array list 39 00:01:33,750 --> 00:01:35,750 ‫and close the semicolon. 40 00:01:35,750 --> 00:01:37,770 ‫So maybe you can come up 41 00:01:37,770 --> 00:01:39,734 ‫with situations like that 42 00:01:39,734 --> 00:01:41,820 ‫where you will see the type 43 00:01:41,820 --> 00:01:45,930 ‫in the second right side of the equation as well. 44 00:01:45,930 --> 00:01:48,750 ‫Like new array list string over here. 45 00:01:48,750 --> 00:01:51,870 ‫You don't have to do that actually, it's deductible 46 00:01:51,870 --> 00:01:53,710 ‫but if you see it like this, 47 00:01:53,710 --> 00:01:55,740 ‫yeah, it's okay as well. 48 00:01:55,740 --> 00:01:56,880 ‫Okay. 49 00:01:56,880 --> 00:01:58,830 ‫You have to be 50 00:01:58,830 --> 00:02:00,600 ‫using this before 51 00:02:00,600 --> 00:02:03,720 ‫but right now it's perfectly okay. 52 00:02:03,720 --> 00:02:06,510 ‫So my musicians dot add, 53 00:02:06,510 --> 00:02:10,200 ‫will add a new element to the array list. 54 00:02:10,200 --> 00:02:11,850 ‫So it'll expect string, 55 00:02:11,850 --> 00:02:14,490 ‫obviously, because we made it to be. 56 00:02:14,490 --> 00:02:17,744 ‫Now I can just say out that print line 57 00:02:17,744 --> 00:02:20,760 ‫my musicians dot get. 58 00:02:20,760 --> 00:02:22,230 ‫And when you say dot get, 59 00:02:22,230 --> 00:02:24,030 ‫it will ask you for an integer 60 00:02:24,030 --> 00:02:25,320 ‫which is an index. 61 00:02:25,320 --> 00:02:27,750 ‫Okay? So, 62 00:02:27,750 --> 00:02:30,180 ‫I'm going to say zero because we are already 63 00:02:30,180 --> 00:02:32,820 ‫have only one over here. 64 00:02:32,820 --> 00:02:36,690 ‫It will bring out to James to me like this. 65 00:02:36,690 --> 00:02:39,210 ‫So I can add as many as I want. 66 00:02:39,210 --> 00:02:40,043 ‫Like I said, 67 00:02:40,043 --> 00:02:43,470 ‫I can say my musicians dot add Lars, 68 00:02:43,470 --> 00:02:48,180 ‫and I can come over here and say my musicians dot add 69 00:02:48,180 --> 00:02:52,830 ‫and actually I can add it anywhere I want as well. 70 00:02:52,830 --> 00:02:56,087 ‫It has some different values, different properties 71 00:02:56,087 --> 00:02:58,350 ‫different methods like this. 72 00:02:58,350 --> 00:02:59,183 ‫As you can see, 73 00:02:59,183 --> 00:03:00,720 ‫you can just specify the string 74 00:03:00,720 --> 00:03:03,000 ‫but you can specify the index 75 00:03:03,000 --> 00:03:04,860 ‫if you want as well. 76 00:03:04,860 --> 00:03:06,420 ‫For example, you can say that, 77 00:03:06,420 --> 00:03:10,080 ‫yeah, add large but add it to this index. 78 00:03:10,080 --> 00:03:12,870 ‫Okay? I don't want it to be added 79 00:03:12,870 --> 00:03:15,630 ‫on the last index that you're available. 80 00:03:15,630 --> 00:03:18,870 ‫I want Kirk on the first index 81 00:03:18,870 --> 00:03:22,770 ‫which is the second element of this array list. 82 00:03:22,770 --> 00:03:26,070 ‫So it will be James, Kirk and Lars 83 00:03:26,070 --> 00:03:27,393 ‫in this case, right? 84 00:03:28,350 --> 00:03:30,960 ‫So let me do it like this. 85 00:03:30,960 --> 00:03:35,640 ‫I'm going to add another one called Rob and 86 00:03:35,640 --> 00:03:36,900 ‫let me just 87 00:03:36,900 --> 00:03:39,420 ‫get everything in this list 88 00:03:39,420 --> 00:03:42,150 ‫so that you can see what's going on, 89 00:03:42,150 --> 00:03:42,990 ‫rather than zero, 90 00:03:42,990 --> 00:03:46,290 ‫I'm gonna say one, two, and three. 91 00:03:46,290 --> 00:03:48,090 ‫And later on, don't worry. 92 00:03:48,090 --> 00:03:50,280 ‫We are going to see a perfect way 93 00:03:50,280 --> 00:03:53,520 ‫to get every element inside of an array list 94 00:03:53,520 --> 00:03:55,350 ‫with a single method. 95 00:03:55,350 --> 00:03:58,383 ‫So here you go, James, Kirk, Lars, and Rob. 96 00:03:59,220 --> 00:04:01,710 ‫If we hadn't had this index thing 97 00:04:01,710 --> 00:04:05,790 ‫then we would have seen James, Lars, Kirk, and Rob, 98 00:04:05,790 --> 00:04:09,150 ‫but this time we see James, Kirk, Lars, and Rob 99 00:04:09,150 --> 00:04:11,580 ‫because we specified Kirk 100 00:04:11,580 --> 00:04:13,950 ‫to be put into the first index 101 00:04:13,950 --> 00:04:18,300 ‫as the second element inside of this array list. 102 00:04:18,300 --> 00:04:21,420 ‫So that's how you work with array lists. 103 00:04:21,420 --> 00:04:22,530 ‫As you can see it, 104 00:04:22,530 --> 00:04:26,490 ‫it's much more flexible when we compare them to the arrays. 105 00:04:26,490 --> 00:04:30,840 ‫You can just not specify any kind of capacity at all. 106 00:04:30,840 --> 00:04:33,450 ‫You can put the elements 107 00:04:33,450 --> 00:04:36,090 ‫in whatever index you want 108 00:04:36,090 --> 00:04:39,720 ‫and it's perfect for working with data 109 00:04:39,720 --> 00:04:42,300 ‫that you are not sure of. 110 00:04:42,300 --> 00:04:44,340 ‫Maybe you're downloading it from internet 111 00:04:44,340 --> 00:04:47,190 ‫and you don't know whether it's going to be ten elements 112 00:04:47,190 --> 00:04:50,490 ‫or 12 elements or 1000 elements. 113 00:04:50,490 --> 00:04:53,190 ‫So you're gonna come across array list 114 00:04:53,190 --> 00:04:55,773 ‫in the Android applications very often.