1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Layouts and Animation for Android Apps

Array Methods and More

Start course
Overview
Difficulty
Intermediate
Duration
4h 7m
Students
1
Description

In this course, you'll start experimenting with XML code and diving deeper into layouts, namely linear and constraint layouts. We'll also look at animations and build a few fun features with them. Then, we'll take a deeper dive into the Kotlin programming language and constraint layouts, before building a fully functioning Tic-Tac-Toe game.

On top of that, we'll then build a second app which can play YouTube videos within it, and you'll learn how to work with APIs and API keys.

Intended Audience

This course is intended for beginners to Android app development or anyone who wants to master coding in Kotlin.

Prerequisites

Since this is a beginner-level course, there are no requirements, but any previous experience with coding would be beneficial.

Transcript

Hello, and welcome back. In the last video, we started looking at arrays and how we could apply them to track the state of our tic-tac-toe game. And in this video we'll dive a little deeper into arrays and check out some methods and other fun things we can do with them. So, I have a couple of arrays or lists over here, commented out, we can bring them down as we need. I'm going to use this one right here to create a list of strings. So, I'm going to copy that, and let's use val. I'll call it names = arrayListof and I have 1, 2, 3, 4, 5, 6 names in here. 

And you can see that I called this val and you may be wondering, well, val, it's a variable and you can't change what's in the variable. Well, I cannot change names to have six slots right here. That space in memory is already allocated, but what I can do is in those memory slots, I can replace what exists. So, if I needed to manipulate this one right here, the value of my first index, I could do that. We'll see where this runs into difficulty later on but for now, that's a good place to start. So, we'll start with say you wanted to know how many names there were in this array. So, how many string objects there are. You can check that using the size property. So, what I'll do is say println(names.size). And if I run it, there you go, it says 6, okay?

Because we have six names in our array. Now what if we wanted to know what the last name was in our array? And this is a small one, so we can see it's Ann, but what if this was like let's say thousands of names? What if we wanted to know the last one? We can check that using the last method. Let's take a look. So, the names.last ( ) ). If I run this, there we go. It says Ann. Now, notice how I referenced this last as a method, but I called size as a property. What's the difference? Well, a property, which we had used size, is one of the defining attributes of the array. So, you created an array and it has a certain size. That was a property of that array. 

However, a method is something that is called when you run a specific piece of code. And here, we ran this last method to determine the last object in our array. And you can usually tell by methods or functions containing these ( ) right after them and they indicate that you're actually calling something. And usually, it's a good way to differentiate between properties and methods, and you'll find out more about that later. Let's now look at another method, and this one is pretty useful. We want to check if a certain name is included in our array, and we can do that using the contains method. So, let's look for John. Let's say this array was 10,000 names and we want to know if John was included. We can say names.contains and within parentheses we provide the name that we're looking for. Within quotes we'll say John, okay? And then close the parentheses. 

Now, if I run this, it says true. That means John is included. Now, if I had looked for Joe instead, let's say Joe, it's false because our array does not contain Joe. Now what if you wanted to display the names in this list in alphabetical order? Basically, you wanted them sorted. How to do that? You can use the sorted method. So, you can remove this and say (names.sorted()). If I run this, there you go. The order changes and now it's sorted in alphabetical order. But if I print the array again, so println(names), you see what happens. It shows the same order as before. First, that's the sorted display, and then looks like nothing has happened to our names array. 

So, sorted, it doesn't actually sort the actual array. It only displays a sorted version of it to you. Another way of putting it is, it doesn't mutate the caller, which is the names array in this case. If you want to mutate the caller and sort the elements in that array, you'd have to use the sort method. So, here instead of sorted, I could say names.sort, run this, whoops, I actually printed that out. So, I won't print this. I'll simply sort it. Now check it out. After sorting, when I print out names it gives me a sorted list, which means the actual list itself has been mutated or changed, okay? Now what if you wanted to work with a sorted order of the array but not change the original names array itself? How would you do that? 

Well, you could assign the sorted array to a new variable and work with that instead. So, you do something like this. You could say val, some variable name. We'll call it sortedNames = names, which is my array right here, .sorted ( ), okay? Now I can println(sortedNames). So, this would be the sorted list, but if I print the original names list, then you'll see that the ordering has not changed. There you go. There is a sorted list but you see the original list remains intact and then we can do whatever we want with this one, okay? Moving on. If you wanted to reverse the ordering of the names or objects in your array, you could use the reverse method. I'll do names.reverse () and then println(names). Let's see what happens. Check it out, it reversed the list. This is not alphabetical, but it just reversed the ordering. 

So, Ann first, then John, then Evgeny, and so on, okay? Notice how also reverse mutated the caller. So, I didn't have to assign this to a new array and print it, but I was able to print names itself and the ordering had been reversed. Now I've been saying this mutating the caller and such quite a bit, and again, this is possible in our case because we used arrayListOf, but there are other array types that are not mutable. For example, if I wanted to create such an array that could not be mutated, I could use listOf instead of arrayListOf, and then I wouldn't be able to reverse it. For example, if I change this to just listOf and I try to do this, let's see what happens. Check it out. We have errors, all right? Let's switch it back to arrayListOf and let's look at adding elements to the list I'll get rid of these two, all right. So, what if I wanted to add a name to this? I could use the add method. 

So, let's say I wanted to add Jessie to this list, I could say names.add and within parentheses and double quotes, I'll provide the name I want to add. Now, if I println(names). There you go. Jessie has been added to the end of my list, but what if I wanted to add Jessie to a specific position in the list, not in the end? Let's say I wanted Jessie as the second element right after Mashrur, that would be Index 1 in my list, right? So, this is Index 0, and then this is index one, and so on. So, if I wanted to add Jessie at index one, then I can provide the index before the string or object I want to add. So, for Index 1, I'll say 1 and then comma, and then the object that I want to add. If I did that and ran it, check it out. Jessie is now at Index 1, okay? Now let's look at removing an element from the list. 

Let's say I wanted to remove John from here, how would I do that? I can simply say names.remove ("Jon"), okay? If I did that, check it out. Jon, without the h, J-O-N is gone. I can also remove by index. So, right here, let's say I didn't know that I wanted to remove Jon, but I wanted to remove whatever is at that index. This is index let's say 0, 1, 2, 3, 4. Let's say I wanted to remove whatever it was at Index 4, then I can use removeAt instead. So, instead of remove, I'll say removeAt and within parentheses, I'll provide the index from which I want to remove. If I run it, you see Jon's gone, all right? Whatever it was at Index 4 is no longer there. Now, one last thing before we conclude this video. 

What if you wanted to remove the last element from the list? And there are a few ways you can do this, but the simplest would be to use removeLast. So, right here I'll say removeLast, okay? If I did that, then Ann should be gone. There you go, the last element from the list is gone. And again, there are a few ways to do this, but that's the simplest one, all right? So, we've covered a fair amount about arrays and how to manipulate their elements in this video. So, we're going to stop here, and in the next video we'll look at iteration and how we can iterate through each element in an array. Basically, we want to visit all the elements in the array, one after the other, and then perform whatever operation we want, all right? Hope you're excited, and I'll see you in the next video.

 

About the Author

Mashrur is a full-time programming instructor specializing in programming fundamentals, web application development, machine learning and cyber security. He has been a technology professional for over a decade and has degrees in Computer Science and Economics. His niche is building comprehensive career focused technology courses for students entering new, complex, and challenging fields in today's technology space.

Covered Topics