In this module, you will learn about collections. Collections are data types that let you store your data in different ways. In this module, you’ll learn more about arrays. Arrays keep track of your information in an ordered list. You’ll learn how to append arrays, delete from arrays, and add elements to a specific number of the array. You will also learn how to enumerate through your arrays.
You will also learn about dictionaries, which is a collection type that uses a key-value pair instead of relying on the specific location in the collection. You will learn how to modify and enumerate through a dictionary.
You’ll also learn about a set, which is simply a collection of unique elements of data without order. Then we’ll spend time with the map method, forEach, filter, and sort. Each of these gives you the ability to change or extract information from your collection in custom ways for your project.
Learning Objectives
- Identify how collections work
- Understand dictionaries, arrays, and sets
- Recognize map, forEach, filter, and sort
Skills you’ll gain
ArraylistData StructuresFunction ModuleJava Collections FrameworkObject StorageSwift (Programming Language)What You'll Learn
- Identify how collections work as data types for storing data in different ways
- Append, delete, and add elements at a specific position in arrays, and enumerate through them
- Work with dictionaries that use key-value pairs, including modifying and enumerating them
- Use sets as collections of unique, unordered elements
- Apply the map, forEach, filter, and sort methods to change or extract information from collections
Key Takeaways
- Collections are data types that let you store your data in different ways.
- Arrays keep track of information in an ordered list, supporting append, delete, and insertion at a specific position.
- Dictionaries are a collection type that uses a key-value pair instead of relying on a specific location in the collection.
- A set is a collection of unique elements of data without order.
- The map, forEach, filter, and sort methods let you change or extract information from your collection in custom ways for your project.
Frequently Asked Questions
What collection types does this module cover?
It covers arrays, which store information in an ordered list; dictionaries, which use key-value pairs instead of relying on a specific location; and sets, which are collections of unique elements without order.
What will I learn to do with arrays?
You will learn how to append arrays, delete from arrays, add elements to a specific number of the array, and enumerate through your arrays.
Which collection methods are taught in this module?
The module spends time on the map, forEach, filter, and sort methods, each of which gives you the ability to change or extract information from your collection in custom ways for your project.
What skills does this course build?
It builds skills in Swift (Programming Language), Data Structures, Java Collections Framework, Arraylist, Object Storage, and Function Module.
Transcript
Show transcript (free preview lesson)
Transcript of the free preview lesson. Remaining lessons unlock with the full course.
Hello, my name is Mark Nair, and in these lessons, you'll learn about arrays, modifying arrays and enumerating arrays, dictionaries, modifying and enumerating them as well, and you'll learn about sets using map and forEach, and using filter and sort. So, let's just get into arrays. An array is an ordered collection of elements. In Swift, we declare an array like this. Now, since I have no content in the array, I have to give it a type, otherwise Swift will complain. And, the type I put into brackets, and that tells me it is a type of array, which is really a full string. So, that's what it looks like. So, here is our array called fruit, which is an array of strings. Now, since Swift can understand that if we have strings in this array, we do not have to specify it here. Just make it simple, like that. Now, arrays can hold any type. So, that means we can make an array that holds numbers, bools, or even functions that act as a type in Swift, like this. So, I have two functions here. One is called add, one is called subtract. Now, I can set up an array, I'll call it math array. And, I could put in the names of those two functions. So, we take a look at what the math array is. There it is, end to end. And that is exactly what we have in int, right here. Let's go back to fruit. To access an arrays element, all we have to do is use brackets and then the position of the element in the array. So, let's say I want to access apple, just put in the position. When I run this, we'll see Apple. Remember, we start at zero. So, 0, 1, 2, which just happened. So, that's two, put in zero, we'll see banana. Let's use a few techniques to inspect our array. We can use several things. We can count the elements in the array. So, in this case, we'll see that we have six elements, 1, 2, 3, 4, 5, 6. We can check if the array is empty. This returns us, this gives us a bool value. In this case, the array is full. So, is Empty is false. We can set a reserve capacity of an array. So, for example, fruit. And, I can say "I want to reserve a capacity of 10 for 10 elements of the array." Oh. So, I cannot do this now because we're technically changing the array. I don't wanna get into modifying too much right now, but I do wanna show you how we can reserve this capacity. So, right now we know that the count is six, but I have told Swift "I wanna reserve for 10". Now, if I take a look at that capacity, and we'll see over here it is 10, even though it's full of six. Now, reserve capacity is really for a little advanced technique on managing your memory and saying "I want this much, reserved this much". That way if you hit the number, Swift doesn't have to make unnecessary copies of your data. Anyway, but it is something in there that you can use. Let's take a look at what the first element of the array is. Banana. And remember, we're using dot syntax for all of this. So, fruit dot first. Now, if I want to know what the index number of apple is in the array, I can use first index, and then of, and I'll just put in Apple. And, that gives me 2. 0, 1, 2. If that makes sense. Let's try to find something in our array. So, we'll use contains. So, I can say fruit contains, Let me try orange. Now, this will give us a boolean value. So, true is the answer, which means, yes. Orange is in the array. Let's do something a little more sophisticated. All right, a quick If statement, here. If the array fruit contains mystery fruit, which I've said is orange, print this, otherwise, print this. And, we run it. There it is. The array contains orange. But, if I come in here and change the mystery fruit to kiwi, there is no kiwi in the array. We can easily find a random element in our collection too. Let's try this. So, a series of colors here. Now I'll say, what random color is equal to, and all we have to do is use the random element method. We could randomize with the number and then bring the number back in. But, this is pretty straightforward. And, I'll just print. Now, this will print something interesting. Let's find out. You should paint your room optional, Violet. So, this is an optional value and the reason we're seeing this is because there is no guarantee that there's anything in the array. And Swift says, "Hey, out of safety, you have to give me something." So, we can fix this with something really quick. Now, what this will do for us is make sure that if we have nothing in the array- let me just get rid of this. I'll have to give it a type, and Swift will complain that it doesn't know what we're doing. Now, if there's nothing in the array and we're trying to get a random element, it ends up with nothing, then it will just default to plaid. Let's take a look at some of the maximum and minimum values in an array. So, an array of different temperatures, Oh, that's hot. Now, I can get the maximum temperature here by simply saying this, hundred and twos, right? I can do the same thing with the minimum, and I could even create a new temperature array and getting rid of the first line, the first element in the sequence here. So, new temp array is equal to temperatures that we just created, and then, I'll use the drop first method here. And now, my new Temp array starts with 12, gets rid of 45.3. But, notice at our previous array of temperatures, still retains that information. Finally, let's set up a new array with some basic info. Let's say we need an array already filled with some values. So, we could do it like this. Let's say you're a teacher, you need array of grades, but you don't know what they are, but you're gonna fill in some basic stuff. So array, and then, right here, I will use repeating in count, repeating, I wanna repeat the number zero, and I wanna do it 10 times. That will give me an array of 0, 0, 0, 10 times. We could make this a hundred, same thing, except a hundred times. If I wanna create an array with values that increment from one to five to five to 10, something like that. Let's do that. But, this time I'll just use a range. There we go. So, here is our array of values one to a hundred going out step at a time, and that's the range right here. Well, thanks for watching. Stay tuned for the next lesson where I'll show you how to modify arrays.
Learn on the Go
Take your learning anywhere — the KnowledgeCity mobile app lets you watch lessons on the go.