Tupel erstellen
Bündeln Sie zusammengehörige Werte.
Tupel erstellen ist eine kostenlose Scala for Backend Engineering & Functional Programming-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Scala for Backend Engineering & Functional Programming-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Scala for Backend Engineering & Functional Programming-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
What Is a Tuple?
A tuple groups a fixed number of values together, even when those values have different types.
Unlike a list, where every element shares one type, a tuple can hold an Int, a String, and a Boolean all at once.
Tuples are perfect for returning or bundling a handful of related values without writing a whole class.
Creating Your First Tuple
You build a tuple by wrapping comma-separated values in parentheses.
Here we pair a name with an age. Scala figures out the types automatically.
val person = ("Alice", 30)
println(person)Mixing Types Freely
One of the best features of tuples is mixing types in a single value.
This tuple holds a String, an Int, and a Boolean together. Each slot keeps its own type.
val record = ("Bob", 42, true)
println(record)Tuple Types
The type of a tuple shows every element type in order.
A pair of a String and an Int has type (String, Int). You can write the type explicitly if you want to be clear.
val pair: (String, Int) = ("Cara", 25)
println(pair)Pairs Are Common
A tuple with exactly two elements is called a pair, and pairs appear everywhere in Scala.
For example, maps are built from key-value pairs. Learning pairs first makes the rest of the language easier.
val score = ("Math", 95)
println(score)Arrow Syntax for Pairs
For two-element tuples, Scala offers a handy arrow syntax.
Writing key -> value is exactly the same as (key, value). It reads nicely, especially when building maps.
val entry = "name" -> "Dana"
println(entry)Larger Tuples
Tuples can hold more than two or three values.
Scala supports tuples up to 22 elements, though small tuples are easiest to read. If you need many fields, a case class is usually clearer.
val data = (1, 2, 3, 4, 5)
println(data)Single-Element Note
There is no useful one-element tuple. Putting one value in parentheses just gives you that value back.
So (5) is simply the number 5, not a tuple. Tuples really start at two elements.
val notATuple = (5)
println(notATuple)Tuples Are Immutable
Once you create a tuple, its values never change.
This immutability makes tuples safe to share and easy to reason about. To get a different tuple, you simply build a new one.
val a = (1, "one")
val b = (2, "two")
println(a)
println(b)Returning Multiple Values
A common use of tuples is returning several values from a function at once.
Here a function returns both a sum and a product as a single tuple, avoiding the need for a separate class.
def stats(a: Int, b: Int) = (a + b, a * b)
println(stats(3, 4))Nesting Tuples
Because a tuple is just a value, you can place a tuple inside another tuple.
This lets you group related sub-pairs together. Keep nesting shallow so your code stays readable.
val nested = (("x", 1), ("y", 2))
println(nested)Quick Check
Test what you learned about creating tuples.
Recap
You learned that tuples group a fixed number of values of possibly different types inside parentheses.
Pairs can also use key -> value syntax, tuples are immutable, and they are great for returning multiple values. Next you will learn how to pull those values back out.
Häufig gestellte Fragen
Ist die Lektion „Tupel erstellen“ kostenlos?
Ja — der vollständige Text von „Tupel erstellen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Scala for Backend Engineering & Functional Programming-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Scala for Backend Engineering & Functional Programming-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Tupel erstellen“?
Bündeln Sie zusammengehörige Werte. Du übst Scala for Backend Engineering & Functional Programming mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Scala for Backend Engineering & Functional Programming zu starten?
Keine Vorkenntnisse erforderlich. Scala for Backend Engineering & Functional Programming auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „Tupel erstellen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Scala for Backend Engineering & Functional Programming-Lektion Code schreiben und ausführen?
Ja. Jede Scala for Backend Engineering & Functional Programming-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Tupel erstellen
- Tupel destrukturieren
- Bereiche erstellen
- Bereiche in Schleifen