# ArrayList in Kotlin Android

## Contents

###### Kotlin Fundamentals
1
2
Variables in Kotlin
PREVIEW12m 15s
3
4
6

## The course is part of this learning path

Start course
Overview
Difficulty
Beginner
Duration
1h 43m
Students
23
Ratings
5/5
Description

This course covers the fundamentals of Kotlin, looking at the building blocks of the programming language and how they can be used to build apps in Android.

### Intended Audience

This course is ideal for anyone who wants to learn how to use Kotlin for developing applications on Android.

### Prerequisites

This content will take you from a beginner to a proficient user of Kotlin and so no prior experience with the programming language is required. It would, however, be beneficial to have some development experience in general.

Transcript

Hello, my friends. So in our previous lesson we introduced the concept of Arrays. So in this lesson, we're going to talk about Array lists. So I'm sure you remember, after we created the array, we couldn't add a new element to the array that we created. But there is a method for this. We need Array Lists. So why don't we just go ahead and have a look at how to create these array lists. So we can create the ArrayList in two different ways, it turns out. The first is very similar to the method that we created in the first or previous lesson here. So after specifying the array name, you could write "arrayListOf" on the right side of the equals.

So, we also need to specify the type of array here. We also need to write the array type between the <>. So that's the first method. In the second method, after defining the name of the array, we write ArrayList to the right of the equals, and again we write the type of the array between the "<>" sign. So, the difference between these two methods is that in the first method you can specify the elements of the array while you're defining the array, or you can add them later. But in the second method, you can add elements to the array only after having created the array. Okay, so in other words here, the choice is up to you. You can choose any of these two methods depending on the application that you're going to make.

So, now let's see how we can add elements to array lists. Since we're doing it right here, we can now use the "add" method for this. Of course, we could use the add method in two different ways. Full of options, we have here. So the first method we can just write the element that we want to add. In this case, it always adds the element that we want to add at the end of the array. In our example here, the last element of the index, that is the third index numbered element would be 20, or we could specify the index number of the element that we will add. So in this case, we would need to write two parameters in parentheses.

The first would be the index number, and the second is the element that we will add. Now we can also delete any element from array lists. And for this we could use a "remove" method. Again, it's possible to use the remove method in two different ways. So in the first method, we can write the element itself. In the second method, we can remove the element from the array by specifying its index number. So, that's all you need to know about array lists.

Why don't we go ahead and move on over to Android Studio and put it into practice? So I just want to create a 'New Kotlin File' and here I'll specify the name of this file to be "ArrayList". Now, let's create our main method. So, first I will create an array list of type "integer". I'm writing "var age equals ArrayList". We'll also specify the type of array between the "<>" marks here. So, here I'll write "Int" and finally I'll put in parentheses. So, we've defined an ArrayList named "age" and type "integer". Now, let's create the elements of this array. On the next line, I will write "age.add". I write 10 in parentheses. And just by doing that, we added an element to the array list named "age".

Now let's add the second element. So this time I'm going to add the index number by specifying it. So I'm writing "age.add" and I'll write 1 in parentheses. So, this represents the index number. Since of course, the number 10 we just added is the first element, the index number was 0. This will be the second element, so the index number must be 1. Now, let's write the value of the second element. So I'm typing 15 in here and thus, we have determined the first two elements of the array list, named "age". So why don't we go ahead and add one more element and print these elements to the console. So I am writing in "age.add". I write 20 in parentheses.

Now, let's print these elements to the console. So I'll type, "sout" and press Enter. And if you remember this method was creating the "printIn" method quite quickly. So I'm writing the First element of age of ArrayList in " marks inside the parentheses and after typing + sign, I'll write "age" and open "[]" brackets. I'll write zero here. So in other words, we print the element of the age array with the zero  index number to the console. Now, I can copy and paste this line twice. So I'll type "Second" here. I'll write "Third" here. Cool.

So now let's print the second and third elements. Now, here I want to show you something a little different writing styles. Okay, so to print the Second element of the console, I'll write "age.get" and I write 1 in parentheses,  and there's really no difference between this type of writing and the way that we're going to write it in the next line. You can use both. So now, let's print the Third element to the console. But here I want to talk about a writing style that I haven't yet mentioned. So I'm going to show you how to write any line of code in a string expression.

Now if you've noticed, whatever we write inside the " marks here, looks exactly the same in the console. So you could say that it's not detected to be code. So if I cut the "age[2]" expression here, and write it in " here, it will appear exactly like this on the console screen. So, we wouldn't be able to learn the Third element of the array. So, what could we do for this expression so that it would be perceived as code? Do you know? Take a guess, or no. That's why you're taking this course.

Okay, well, I'll continue. But, what would happen if you put a \$ sign at the beginning of an expression that you want to be treated as a code in " marks, and then and close that expression in "{}". The expression after the \$ sign will be treated as code. Now, we don't use this spelling very often. It is useful to know. So, now let's run and test our code. So as you can see we've printed three elements of the "age ArrayList" to the console. Now, if you want to use the "remove" method, I wonder if you remember, we could remove any element of the array from the array list with a remove method. For example, let's just remove the Second element of the array 15 from the array. So for this array "age.remove". And if you use the remove method, you must directly write the element itself in parentheses.

So if you use the 'removeAt' method then you must write the index number of the array you want to remove in parentheses. So I'm using the 'remove' method right now. You can go and test the other method yourself but I want to show you this. So write 15 in parentheses. Of course, when we delete the Second element of the array from the array, the Third element of the array 20 will now become the Second element of the array. So now, let's first print the size  of the array to the console. So for this I can write "age.size" in parentheses.

Now let's print the First and Second elements of the array. For this, I can copy and paste the above lines of code here. Now also before running the code, let's convert these lines of code to the comment line. So now we can run the code, and we should be able to see the numbers 2, 10 and 20 on the console. And as you can see, we have printed the size of the array. It's First and Second elements, right here to the console. So now, let's try this. We'll create an array using the "arrayListOf" statement. And this time we're going to create an array of type "String". So I'm writing "var cars equals arrayListOf" the type of array will be "String", and now we can specify the first two elements of the array here.

So I write Opel in parentheses as the First element. Second element, I'm going to write BMW. Now we can add the Third element of the array using the "add" method. So here I'll write "cars.add". I'll write Ford in parentheses. Now let's print the elements of the array to the console. So for this, I'll copy and paste the lines of code that I wrote above, and I should write "cars" instead of "age" here, because the name of our array is cars. Good catch. Finally, let's run and test our code. So as you can see, we printed Opel, BMW, and Ford on the console, and as you can see guys, we can create array list using both methods. That's cool.

Now, I do want to show you one more thing and then we'll wrap this lesson up. Okay? So if you remember in our array lesson we created an array containing both string and integer expressions. And yes, this is also possible with array lists, my friend. So in other words there may be different data types in the array that you want to create. Totally doable. But for this my friends, you will need to define the type of the array as "Any".

All right, so let me just show you a quick example. So here I am writing "var myMixArrayList" equals, so after writing an ArrayList, I set the type of the array to "Any". Now let's add an element to the array. Let the first element be of type "String". So here I write myMixArrayList.add. I write Ford in parentheses. Let the second element be of the type "integer". So I write myMixArrayList.add, I write 5 in parentheses. And we'll make the third element be of the type "double". I write myMixArrayList.add. I'm writing 2.5 in parentheses. Let's make the fourth element be of the type "Boolean". So I write myMixArrayList.add. I write false in parentheses. Now the fifth and final element will be of the type "Char". So I write myMixArrayList.add. I write K in single quotes in parentheses. So I 'll just quickly print these statements to the console, so we get some writing "myMixArrayList[]", zero inside the bracket.

Now let's copy this line and paste it four times. Now let's edit inside the "[]" bracket. Finally, I write 4. All right so now, let's run and test our code. And as you can see, we have printed the elements of the array list which consists of different data types, right here to the console. All right my friends, so that's how you use array lists. I'll tell you we're going to be using array lists a lot. Just a heads up when we're doing our Android applications, so get used to it. And hopefully you get the logic. Of course, we're only showing you just the bare bones of it. So I might have heard a couple of snores out there. Not sure. But when you start developing your android apps using this information you've learned, Oh boy, you're going to see how it makes it, such a joy, truly. So, just be a little patient in the beginning, and learn it all and we'll just have a short break here. Want to see you in the next lesson, which starts in the next video. See you then.