Arrays in Android App (Kotlin)

Start course
Overview
Difficulty
Beginner
Duration
1h 43m
Students
23
Ratings
5/5
starstarstarstarstar
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

Alright, So hello my friends. Of course, in our previous lesson, we learned about the string data types, on this lesson, we're going to learn about Arrays. Now, I'm sure you remember when we were talking about data types. We said that there is also an array data type. So, just what are these arrays, and what are they used for?

Well, arrays are actually objects that hold multiple data of the same type together. So, it's very useful in solving problems involving many variable as it can store the same type of data within itself. I'll tell you what, let me just show you an example. Let's say that you store the names, ages and addresses of employees in a company in a database. This is where you keep all of this information. So, consider that there are perhaps a 1000 workers working in this company. So, in other words, there's going to be 1000 names, a 1000 ages, addresses all registered in the database. Alright, so a 1000 is just a small round number, but it's huge.

So let's say that you want to know the name of all the different workers. In this case, you could create a variable that you will pass each name into and then act on these variables, but what's that going to do? Well, first of all, involves a lot of work. It's actually quite difficult because in this case, you would need to create a 1000 variables. So, that's why in this case we would use an array. In other words, instead of creating 1000 variables, we could create one array and get the names of a 1000 workers from the database. Does that make it a little clearer?

So, now let's see how we can actually create an array. So, first of all, we do want to determine what are we going to call this array, and we specify the element of our array using the "arrayOf" method. So, if you examine the example here, we've got an array called "age". This array has seven elements of "integer" data type. So, each element in an array is also represented by an "index". In other words, the first element of the array is actually the element whose index number is zero. The fifth element. Well, of course, it's an awesome movie, but the fifth element of the array is the element with the index number of four. So, you follow the last seventh element of the index is the element with the index number of six. So, that is, the index number starts from zero. The final index number is one less than the number of the elements of the array. So that's enough of that information. Let's move on to Android Studio and practice it. You'll pick it up really quick.

So first, I want to create a new Kotlin file. I'll just specify the name to be "Arrays". So, now let's create the main method. Now we can create an array called "age". So I'm typing "var age equals", after typing "arrayOf". We can write the elements of the array in parentheses; to also use "," to separate elements from each other. As you can see, we have an array here of three elements called "age". Now this zeroth index of this array has one. The first index has two. The second index has three.

Let's print these elements to the console; I'll write "sout" and press 'Enter'. So, print the first element of the array. So I'll write the First element of the Array equals between " marks. After the + sign, write the name of the Array and write the age. Then I'll write the index number of the element that we want to show in the console, which in this case is zero, inside the "[]" brackets.

So, now let's print the Second and Third elements in a similar way. What I'll do is, I'll copy and paste this line of code twice, and then we can break in and do some editing. So I'll write one inside the brackets. So, that will be the Second element of the Array. In fact, to get the element of the Array at any index, we can use the "get" method rather than writing it this way. So, I'm going to show that real quick. In other words, after typing the name of the array, if you write ".get" and write the index number in parentheses, you can learn the elements of the array. So there's no difference between these two spellings actually, you can use either one. I'm also writing two here. So it'll be the Third element of the Array. All right, so now let's run our code. And there you go. You can see that we've printed all the elements of the array to the console, and that's good.

So, now let's create an array of the type "String". So in here, I'll just write "var cars equals". After typing the "arrayOf", I'll write the elements of the array in parentheses. So, let's the First element be Mercedes. And notice that I always write string expressions between " marks. So, let the Second element be BMW. Let's make the Third element Opel. And now let's print these elements to the console. So for that, what I'll do is copy and paste the above methods here. And let's replace age with cars because you know, our array is called "cars". Now also, in order to not mix with the elements of the above arrays, I'm going to make a partition here. I will divide by using the -- and parentheses. That looks good. So, now we can run our code, and there you see, my friends, we have printed the elements of the "age array" and the elements of the "cars array" to the console.

So, now we can look at some other operations that we can do in arrays. First of all, let's try to replace the Third element of the cars array. So in other words, when we first created the array. We set the Third element of the array as Opel. Now, we're going to try to make it Ford. So, we're going to use the "set" method for this process. So, after typing the name of the array, if you put a dot, you will see a number of options provided to us by the code editor, and when we write "set" here, it's going to ask us for two parameters. First parameter is the index number of the element that we want to change. The second parameter is the new value of the element. So, for the first parameter, I'll just type in two because the index number of this Opel element is two. I'll write Ford as the new value. So, now let's "print" the elements of the array again. I'll copy methods from here, paste them in here. And now let's run the code. And look at that. As you can see the Third element of the cars array was Opel, and after changing it using the "set" method, it became Ford.

Now, I also want to show you how we can find out the size of arrays because sometimes you can develop some applications by retrieving data from the internet or database. And in these cases, you might not know the size of the arrays initially, and believe me, you do want to find out. So for this, we always use the "size" method. So, I'll show you if you write "cars.size" here. Well, you can find out the size of the cars array and now let's print it to the console. So, I'm cutting this out and putting it in parentheses, and before it between double quotes, I write "Size of cars array", and I'll put the + sign in to combine these two expressions. So, let's run the code. So as you see, we saw the number three on the console.

So finally, I want to show you one more thing, and that is, so when you want to create an array using the "arrayOf" method, the elements of the array can be of different data types. So, we don't have to specify the elements of the array just as integers or just as Strings or just doubles or something. So, in other words, the types of elements in the array can be different.  So, I will reinforce this idea with an example. I am going to create an array called "carsAndAge". So, after writing the "arrayOf" method. I'll write Mercedes as the First element. I'll write 5 as a Second element, Opel as the Third element, and 10 as a Fourth element in parentheses. So, now let's bring the elements of this array onto the console. So, I'm copying the "printIn" methods here and placing them here. Now I'm going to copy and paste one more time because the array consists of four elements. Now, let's organize our methods a little bit. So, I'm typing Fourth here, and I am now correcting the array's name to "carsAndAge". Finally, I'll write three here. Now let's run the code. So, as you can see, we have printed the elements of the array to the console. So, you see how just by doing it this way you can also create array using different types of data types. All right, so that's pretty cool, my friends. So far we have learned how to create an array, how to access the elements of the array, how to change the element in any index and how to find out the number of elements in the array itself. Just when you thought we were wrapping up, I do want to take you one step further.

Now, I want you to notice that when we create an array, we have determined its elements from the very beginning. Well, what do you think? Could we add an element to an array that already exists? Or perhaps if we can, how do we do that? Well, let's have a look. So let's take for example, the latest "carsAndAge" array that we created. So, this is an array consisting of four elements. So why don't we try to add the fifth element to this array? You haven't seen it, you've got to. So first, I'll write the name of the array.

Now, if you remember, when we were presented with a number of options, after putting a dot, we just went ahead with it. But I want you to just watch along with me or do it along with me, depending upon your skill level. So, let's just scroll through and see if there's a method, like "add" or "put". So no, we can't actually see such a method. So, what do you think? Could we do that with a "set" method? Well, let's try. All right, so after writing the "set" here, let's write the index number and the element that I will add in parentheses, writing 4 as the index because, the index number of the fifth element will be four. So let's write 20 as the fifth element. If you want to, go ahead and run the application and test it out. But as you can see, you're going to get an error. So this error is called "ArrayIndexOutOfBoundsException". So, we try to exceed the limits of the array, but when we did that, we encountered an error. So, what do you think we should do if we want to add an element to an array later and not encounter an error? On this case, we could use "Array lists". So, in other words, it is possible to work a little bit more flexibly by using Array lists. So, why don't we just break here for just a short time, and we'll learn about Array lists in the next video. Cool, so I'll see you then.

 

About the Author
Students
97
Courses
23
Learning Paths
1

Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.

Covered Topics