A tuple is a small ordered collection of objects, where each object can have a different type. Unlike lists and arrays, however, there is no way to iterate through elements in a tuple. Its purpose is only as a container for more than one value. Learn more
- [Narrator] Let's talk about the concept of tuples. A tuple is a small, ordered collection of objects where each object can have a different type. Unlike lists and arrays, however, there is no way to iterate through elements in a tuple. It's purpose is only as a container for more than one value. We can create tuples is two ways. Let's take a look. We can type in val and let's just call it tuple is equal to in parantheses I can put some values. Such as one, false, and Scala.
Now when I save this we'll see this creates a tuple. And on the right hand side it tells me my tuple has an Int, a Boolean, and a String. The next way to create a tuple is to actually show the ordered pairs. So this time let's do tuple two is equal to, and let's use a String, so let's say, computer. And we're going to use the right facing arrow. Which is the relation operator to say the computer is related to And on the right hand side let's say Dell Pavilion Desktop.
Again when I save this, we'll see on the right hand side, let me scroll over a little bit. We have tuple, the first one, which has Int, Boolean, String and the second tuple has String and String. And then it lists the values inside the tuple. We can access an individual element in a tuple. We just can't traverse it. So let's say I wanted to get at the second value in my tuple two. I can say val second is equal to and I can use the name of my variable tuple two and then I can do dot underscore two.
And that will access the element using the index. Now what's a little different here with tuples is that tuples start with the index value of one. So it's a little unusual because arrays and lists start at zero. Let's go ahead and save this and we should see that the second element is the Dell Pavilion Desktop. So it looks like that is working. Tuples can contain anywhere from two to 22 elements. And they're associated with a type based on the number of elements.
For example a two element tuple with an Int and a String has the type tuple two and string. You can create a tuple two or a pair as I showed you using the right facing arrow operator which works on any value. Let's try another one. Let's say val t is equal to and let's say a and we'll use an arrow and we'll say 25. And we save this. And we'll see it created a tuple that has a String and an Int.
And on the right hand side it shows a comma 25. We could've written this easier, we could've just said val t equals parantheses double quotes a double quotes comma 25 and that's the same thing. We can also create tuples using functions. Let's define a function that will break a number into the tens digits and the ones digit. So I'm going to define one, I'll call it div 10, to divide 10. And it takes n an integer, we'll call that integer n.
And it's going to return a tuple two that has two integer values. And the code that I need to find those I'm going to use divide by 10 to get the tens digit because it's passing in an integer. If I divide it by an integer, I'll get just the integer. So let's do that. Parantheses n divided by 10 comma and then in order to get the ones digit I use the remainder operator. So n percent sign 10. And this will create a tuple when I pass in a value it'll return back to me the tens and the ones digit.
So let's create a variable. Let's call it val and this time I'm going to declare two variables, a tens variable and a ones variable. And I'm going to say that that is equal to calling the divide 10 with a number, let's go with 85. And finally let's use a print line statement to print out our tens digit. And we'll use a second one to print out the ones digit. Okay I'm just going to add some spaces to make it look nice. It's not required for Scala.
Now remember when I save this it will actually execute the code. So let's give it a try. Control s because I'm using a Windows machine, and you can see on the right hand side it did print out the tens digit as eight and the ones digit as five. So this was just a quick example of what tuples are, how to create them, how to actually access the values inside a tuple, and even how to write a function that allows us to use the tuple to divide a number into the tens digit and the ones. Try using this as a guide and expand this to three digit numbers.
Print out the hundreds, the tens, and the ones value.
- Installing Scala
- Integrating with IDEs
- Creating variables
- Using special functions
- Basic types and operations
- Tuples, sets, maps, arrays, and lists
- Classes, fields, and methods
- Singleton objects