시작하기무료로 시작하기

Initialize and prepend to a list

The List in Scala is like the Array in that they are a sequence of objects that share the same type. The List collection, however, is immutable. When performing operations on lists, the result will be a new list with a new value. The cons operator (::) prepends an element to a list.

Imagine you wanted to raise the stakes a little when playing Twenty-One by adding a monetary prize for each round. The winner of round one gets $10, the winner of round two gets $15, etc.

In this exercise, you'll create a list to store these prize values for five rounds, then prepend to it to accommodate another round.

이 연습은 강의의 일부입니다

Introduction to Scala

강의 보기

연습 안내

  • Initialize a list named prizes with an element for each round's prize, where the first through fifth round's prizes are 10, 15, 20, 25, and 30, respectively.
  • Prepend to prizes using the cons (::) operator so a new first round is added, worth $5. Name the new list newPrizes.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

// Initialize a list with an element for each round's prize
val ___ = ___
println(prizes)

// Prepend to prizes to add another round and prize
val ___ = ___
println(newPrizes)
코드 편집 및 실행