Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Compile and Execute Scala Online

  import java.math.BigInteger
  import scala.collection.mutable.Map
  import scala.collection.mutable.Set
  object HelloWorld {
     def main(args: Array[String]) {
     def max2(x: Int, y: Int) = if (x > y) println(x) else println(y)
       max2(4,5);
         println("Hello, "+ args(0) +"!")
         var res: Int = 5;
         res = res * 3
         println(res)
         def greet() = println("Hello, world!")
         greet();
           var i = 0
            while (i < args.length) {
             println(args(i))
            i += 1
            } 
            var j = 0
        while (j < args.length) {
            if (j != 0)
             print(" ")
                print(args(j))
                j += 1
            }
         println()
         args.foreach(arg => println(arg))
         args.foreach((arg: String) => println(arg))
          args.foreach(println)
           for (arg <- args)
             println(arg)
               val big = new java.math.BigInteger("12345")
               println(big)
    
  val greetStrings = new Array[String](3)
    greetStrings(0) = "Hello"
  greetStrings(1) = ", "
  greetStrings(2) = "world!\n"
  for (i <- 0 to 2)
    print(greetStrings(i))
  greetStrings.update(0, "Hello")
  greetStrings.update(1, ", ")
  greetStrings.update(2, "world!\n")
  for (i <- 0.to(2))
    print(greetStrings.apply(i))
    greetStrings(0) = "Hello" 
    greetStrings.update(0, "Hello")
    println(greetStrings(0))
    val numNames = Array("zero", "one", "two")
    val numNames2 = Array.apply("zero", "one", "two")
         val oneTwo = List(1, 2)
  val threeFour = List(3, 4)
  val oneTwoThreeFour = oneTwo ::: threeFour
  println(""+ oneTwo +" and "+ threeFour +" were not mutated.")
  println("Thus, "+ oneTwoThreeFour +" is a new list.")
   val twoThree = List(2, 3)
   val oneTwoThree = 1 :: twoThree
   println(oneTwoThree)
 val oneTwoThreee = 1 :: 2 :: 3 :: Nil
  println(oneTwoThreee)
   val pair = (99, "Luftballons")
        println(pair._1)
        println(pair._2)
        var jetSet = Set("Boeing", "Airbus")
  jetSet += "Lear"
  println(jetSet.contains("Boeing"))
    val movieSet = Set("Hitch", "Poltergeist")
  movieSet += "Shrek"
  println(movieSet) 
    val treasureMap = Map[Int, String]()
  treasureMap += (1 -> "Go to island.")
  treasureMap += (2 -> "Find big X on ground.")
  treasureMap += (3 -> "Dig.")
  println(treasureMap(2))
  val romanNumeral = Map(
    1 -> "I", 2 -> "II", 3 -> "III", 4 -> "IV", 5 -> "V"
  )
  println(romanNumeral(4))
  def printArgs(args: Array[String]): Unit = {
    args.foreach(println)
              }
              printArgs(Array("zero", "one", "two"))
       def printArgs1(args: Array[String]): Unit = {
            var i = 0
            while (i < args.length) {
                  println(args(i))
             i += 1
               }
          }
     printArgs1(Array("zero", "one", "two"))  
     def printArgs2(args: Array[String]): Unit = {
    for (arg <- args)
      println(arg)
  }
        printArgs2(Array("zero", "one", "two"))  
         def printArgs3(args: Array[String]): Unit = {
        args.foreach(println)
  }
          printArgs3(Array("zero", "one", "two"))  
           def formatArgs(args: Array[String]) = args.mkString("\n")
            println(formatArgs(args))
     val res1 = formatArgs(Array("zero", "one", "two"))
        assert(res1 == "zero\none\ntwo")
       }
  }

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.