1 00:00:00,480 --> 00:00:01,290 ‫Welcome back. 2 00:00:01,320 --> 00:00:07,190 ‫In this video, we are going to cover a red list and at least some parts of it. 3 00:00:07,200 --> 00:00:10,260 ‫And let's go ahead and start by creating an ArrayList. 4 00:00:10,260 --> 00:00:14,340 ‫So I'm just going to create a new array list here, declare it. 5 00:00:14,340 --> 00:00:18,150 ‫So declaring an array list. 6 00:00:18,750 --> 00:00:22,650 ‫And as you can see, it's complaining because it doesn't know array list. 7 00:00:22,650 --> 00:00:32,340 ‫And we need to use system dot collections using system dot collections because the array list class 8 00:00:32,610 --> 00:00:37,440 ‫is within the collections class, as you can see, implements the list interface and so forth. 9 00:00:37,440 --> 00:00:43,470 ‫So now I can go ahead and give it a name and I'm just going to use it or call it my array list is a 10 00:00:43,470 --> 00:00:45,750 ‫new array list. 11 00:00:45,930 --> 00:00:55,740 ‫So that's one way of declaring an array list and another way is to already say how many objects should 12 00:00:55,740 --> 00:00:56,520 ‫be in there. 13 00:00:56,700 --> 00:01:01,680 ‫So in this case I'm going to say, okay, my array list should contain 100 objects. 14 00:01:02,400 --> 00:01:14,100 ‫This one here has an indefinite amount of objects with an indefinite defined amount of objects, and 15 00:01:14,100 --> 00:01:19,230 ‫that one is with a defined amount of objects. 16 00:01:21,070 --> 00:01:23,530 ‫With a defined. 17 00:01:23,740 --> 00:01:31,690 ‫All right, so now let's go ahead and add some objects or elements to the array list. 18 00:01:31,690 --> 00:01:33,790 ‫So my array list, dot add. 19 00:01:33,970 --> 00:01:41,380 ‫As you can see, my array list has multiple different methods, properties and extension methods. 20 00:01:41,380 --> 00:01:47,290 ‫So you can check some of those out and you can even see something like Clear will simply clear the whole 21 00:01:47,290 --> 00:01:47,920 ‫array list. 22 00:01:47,920 --> 00:01:49,120 ‫Then you have contains. 23 00:01:49,120 --> 00:01:53,440 ‫It will check whether there is something in there that you're searching for and so forth. 24 00:01:53,440 --> 00:01:56,650 ‫So plenty of interesting methods and we're going to cover some of them. 25 00:01:56,650 --> 00:02:00,430 ‫So I'm going to add something. 26 00:02:00,430 --> 00:02:02,680 ‫And as you can see, I can add an object. 27 00:02:02,680 --> 00:02:04,510 ‫And that's the beauty about array lists. 28 00:02:04,510 --> 00:02:11,650 ‫They don't need to contain strings only or only or anything else only they can contain objects. 29 00:02:11,650 --> 00:02:16,810 ‫So anything that is of any type of object can be used here. 30 00:02:16,810 --> 00:02:23,020 ‫So that means we can store integers in there, let's say the value of 25, then we can go ahead and 31 00:02:23,020 --> 00:02:31,990 ‫store strings in there, something like Hello, or we can go ahead and store doubles in there. 32 00:02:31,990 --> 00:02:36,880 ‫So 13.37 and so forth. 33 00:02:36,880 --> 00:02:49,330 ‫So I'm just going to add some additional values in here, like 13 my array list that ADD and 128 and 34 00:02:49,330 --> 00:02:54,130 ‫just another value to be sure will be 25.3. 35 00:02:55,780 --> 00:02:57,040 ‫And that's not a comma. 36 00:02:57,040 --> 00:02:58,180 ‫That's a point. 37 00:02:59,920 --> 00:03:00,580 ‫All right. 38 00:03:00,910 --> 00:03:06,250 ‫So now what I can do is I can use some methods onto my array lists. 39 00:03:06,250 --> 00:03:09,520 ‫For example, I can use the remove method. 40 00:03:09,520 --> 00:03:13,450 ‫And in here I need to define what I want to remove. 41 00:03:13,450 --> 00:03:18,040 ‫So I'm not just removing an index, I'm saying which object I want to remove. 42 00:03:18,040 --> 00:03:24,700 ‫So let's say I want to get rid of the 13, so I'm just going to use remove 13. 43 00:03:24,700 --> 00:03:39,580 ‫This will delete element with specific entry from the array list or with specific value. 44 00:03:39,610 --> 00:03:40,870 ‫Maybe that's better. 45 00:03:44,170 --> 00:03:46,420 ‫So it's going to remove 13 from there. 46 00:03:46,420 --> 00:03:54,220 ‫So let's say I have two times 13 in there, which I'm going to do like that, then it will remove 13 47 00:03:54,220 --> 00:03:55,240 ‫from my list. 48 00:03:55,900 --> 00:04:10,450 ‫Now the next one is to remove at a specific index, so delete element at specific position or index. 49 00:04:10,450 --> 00:04:14,530 ‫And that is done by using the remove ET method. 50 00:04:14,530 --> 00:04:17,020 ‫And you can see there is a remove range. 51 00:04:17,020 --> 00:04:21,370 ‫So you can simply say, okay, from that point I want to delete two values. 52 00:04:21,370 --> 00:04:23,200 ‫So let's say from 0.4. 53 00:04:23,200 --> 00:04:26,980 ‫So this one here, I want to delete two values. 54 00:04:26,980 --> 00:04:30,400 ‫So it's going to delete 128 and 25.3. 55 00:04:30,520 --> 00:04:35,770 ‫But I'm just going to use remove ADD and I'm going to remove the one at position zero. 56 00:04:35,770 --> 00:04:39,640 ‫So it should delete or remove this one here. 57 00:04:39,640 --> 00:04:45,970 ‫So the 25 and now there is something called count. 58 00:04:45,970 --> 00:04:52,900 ‫So if you want to know how many objects are inside of your array list, you can use the count keyword. 59 00:04:53,200 --> 00:04:59,320 ‫Now what I want to do is write that onto the console, so I'm just going to use it like that. 60 00:04:59,320 --> 00:05:02,680 ‫So write console count. 61 00:05:03,190 --> 00:05:05,950 ‫All right, now let's get to something very interesting. 62 00:05:07,210 --> 00:05:12,910 ‫Let's create a for each loop which will check all the objects in our array list. 63 00:05:12,910 --> 00:05:17,890 ‫And I'm going to call it object or object in my array list. 64 00:05:18,040 --> 00:05:23,950 ‫And it's going to go through every single object into inside of my array list, and it's going to be 65 00:05:23,960 --> 00:05:25,960 ‫object in that iteration. 66 00:05:25,960 --> 00:05:33,790 ‫So what we can do is we can check if object is of type int, then we can do some coding. 67 00:05:33,790 --> 00:05:39,670 ‫So let's say I want to add up all the numbers and of course I don't want to add the string in here, 68 00:05:39,670 --> 00:05:44,890 ‫so I'm going to create a new double result. 69 00:05:47,050 --> 00:05:50,200 ‫Or you could call it some, maybe some is more precise. 70 00:05:50,200 --> 00:05:51,580 ‫This will be the sum. 71 00:05:52,000 --> 00:06:00,550 ‫And now my sum should be the integer and the double that are inside of my array list. 72 00:06:00,550 --> 00:06:06,010 ‫But now as I can simply add an integer to a double, I need to convert it. 73 00:06:06,010 --> 00:06:09,970 ‫So I'm going to use to convert that to double. 74 00:06:10,480 --> 00:06:18,160 ‫And here I'm going to say object because as object is an object or object is an object. 75 00:06:18,160 --> 00:06:19,540 ‫It's not a integer. 76 00:06:19,540 --> 00:06:22,210 ‫It contains an integer, but it's of type object. 77 00:06:22,210 --> 00:06:26,410 ‫So I can cast it into a double and simply add it to a double. 78 00:06:26,410 --> 00:06:30,280 ‫But if it's not a double, I need to convert it into a double. 79 00:06:30,280 --> 00:06:33,400 ‫So if it's an integer, I can convert it to a double. 80 00:06:33,400 --> 00:06:34,180 ‫So I'm checking. 81 00:06:34,180 --> 00:06:34,990 ‫Is it an integer? 82 00:06:34,990 --> 00:06:35,650 ‫Yeah, it is. 83 00:06:35,650 --> 00:06:40,270 ‫All right, so let's convert it into a double and let's add it to our sum. 84 00:06:40,480 --> 00:06:45,700 ‫And here else, if J is a double. 85 00:06:46,390 --> 00:06:52,120 ‫So if that's the case, then simply use some plus equal. 86 00:06:53,020 --> 00:06:54,910 ‫And here you can't just say OBJ. 87 00:06:54,940 --> 00:07:01,780 ‫As you can see, I can simply add the object that has to do with this being an object and not a double. 88 00:07:01,780 --> 00:07:04,780 ‫And you cannot simply add an object to a double, as I just said. 89 00:07:04,780 --> 00:07:07,750 ‫So what you need to do here is to use casting. 90 00:07:07,750 --> 00:07:12,100 ‫So in here I'm going to say I want to cast that object into a double. 91 00:07:12,100 --> 00:07:17,680 ‫And if that's done, then please add that to the sum and I'll finally else. 92 00:07:18,310 --> 00:07:23,920 ‫And here could also of course check if J is string. 93 00:07:25,780 --> 00:07:30,370 ‫So if that's the case, then simply write it onto the console. 94 00:07:30,370 --> 00:07:31,690 ‫So just write. 95 00:07:31,690 --> 00:07:41,440 ‫OBJ All right, now let's write the console or use the console to see what the result was. 96 00:07:42,160 --> 00:07:54,670 ‫Result and not with a capital and I actually call the sum and finally console the right key. 97 00:07:56,130 --> 00:07:57,150 ‫Now let's run it. 98 00:07:57,840 --> 00:07:59,490 ‫And as you can see, we get five. 99 00:07:59,490 --> 00:08:02,910 ‫We get hello and 179.67. 100 00:08:02,910 --> 00:08:04,540 ‫So what are these numbers? 101 00:08:04,560 --> 00:08:09,840 ‫This five is the amount of elements inside of my ArrayList. 102 00:08:09,840 --> 00:08:12,450 ‫So it says here, count and count is five. 103 00:08:12,480 --> 00:08:17,400 ‫If we check out here how many values we had while we had seven, one, two, three, four, five, six, 104 00:08:17,400 --> 00:08:17,880 ‫seven. 105 00:08:17,880 --> 00:08:23,940 ‫And we removed the 13 and we removed the zero. 106 00:08:23,940 --> 00:08:26,700 ‫So now let's check out if the 13 is still in there. 107 00:08:26,700 --> 00:08:38,310 ‫So if we take that 13.37 and we add 128 and 25.3 plus the 13, as you can see, it gives us this result. 108 00:08:38,310 --> 00:08:44,850 ‫So deleting remove 13, it deleted the element with a specific value. 109 00:08:44,850 --> 00:08:49,110 ‫So deleted one element and not all of them as you can see here. 110 00:08:49,110 --> 00:08:53,040 ‫So it's removing a 13 and it just removing the first one. 111 00:08:53,340 --> 00:08:55,530 ‫So let's copy that and run it again. 112 00:08:56,510 --> 00:08:58,580 ‫And now it will remove 13 twice. 113 00:08:58,580 --> 00:09:01,910 ‫And as you can see, we get this value of 166. 114 00:09:01,910 --> 00:09:04,640 ‫So there was still a 13 in there. 115 00:09:04,640 --> 00:09:07,610 ‫So let's run it again when there is no 13. 116 00:09:08,960 --> 00:09:09,680 ‫And. 117 00:09:11,110 --> 00:09:12,610 ‫As you can see, all is good. 118 00:09:12,610 --> 00:09:15,480 ‫So it tried to remove something, but that was nothing. 119 00:09:15,490 --> 00:09:19,150 ‫So if there is nothing, as you can see, it just doesn't do anything. 120 00:09:19,150 --> 00:09:27,040 ‫Even though we tried three times to delete this 13 or remove the 13 and there was no 13 in there anymore. 121 00:09:28,600 --> 00:09:28,990 ‫All right. 122 00:09:28,990 --> 00:09:32,500 ‫So that was a little start into ArrayList. 123 00:09:32,500 --> 00:09:36,070 ‫And as you can see, there is plenty to know about ArrayList and they're pretty cool. 124 00:09:36,070 --> 00:09:41,710 ‫And the next video, we're going to go ahead and use even more methods of the ArrayList class. 125 00:09:43,030 --> 00:09:48,010 ‫Before we do that, however, I would like to quickly talk about something that I haven't talked about. 126 00:09:48,010 --> 00:09:50,080 ‫And this is this object here. 127 00:09:50,080 --> 00:09:57,340 ‫So why am I using object in my ArrayList here instead of, for example, ArrayList as the data type 128 00:09:57,340 --> 00:09:59,830 ‫or even integer or string? 129 00:09:59,950 --> 00:10:04,420 ‫Well, I'm using object here because object is the highest level. 130 00:10:05,150 --> 00:10:11,390 ‫Class that there is every other class is in a certain way inheriting from that class object. 131 00:10:11,420 --> 00:10:18,020 ‫That's why we can use objects here, because any different data types that will be within my array list 132 00:10:18,020 --> 00:10:19,340 ‫will work with this. 133 00:10:19,340 --> 00:10:25,310 ‫Because if we look at our array list, we have an integer here, we have the string here, then we have 134 00:10:25,310 --> 00:10:26,410 ‫a double in there. 135 00:10:26,420 --> 00:10:32,750 ‫So with three different data types in there and we cannot just say, okay, object or this object is 136 00:10:32,750 --> 00:10:38,360 ‫going to be an integer or it's going to be a string or a double, because that wouldn't be correct, 137 00:10:38,360 --> 00:10:40,940 ‫because it's all of those things, right? 138 00:10:40,940 --> 00:10:43,460 ‫And object takes just care of that. 139 00:10:43,460 --> 00:10:51,230 ‫So if we use this approach, then it's going to be smart enough to understand OC in case it's going 140 00:10:51,230 --> 00:10:57,230 ‫to be an integer, it's going to take care of that and it's going to add to the sum. 141 00:10:57,230 --> 00:11:00,350 ‫Otherwise it's going to add to the sum if it's a double. 142 00:11:00,350 --> 00:11:02,790 ‫But if it's a string, it's just going to print out the string. 143 00:11:02,810 --> 00:11:03,140 ‫All right. 144 00:11:03,140 --> 00:11:05,990 ‫I just wanted to make sure that this is totally clear. 145 00:11:05,990 --> 00:11:08,900 ‫That's why I added this little piece to the video. 146 00:11:08,900 --> 00:11:13,970 ‫If you have any other questions where you feel like it was not explained well enough, then please let 147 00:11:13,970 --> 00:11:18,920 ‫me know in the Q&A and I will add some additional instructions or. 148 00:11:19,220 --> 00:11:22,340 ‫Yeah, descriptions to the lectures. 149 00:11:22,370 --> 00:11:22,760 ‫All right. 150 00:11:22,760 --> 00:11:24,470 ‫So see you in the next video.