Scala Self-Study Diary (6)-array initialization and looping

Source: Internet
Author: User

The array inside the 1.scala is similar to that in Java, but there are some different ways of initializing the array, which is a bit more than Java, as follows:

The code is as follows:

Package Com.scala.demo

/**
 * Here is the main introduction to the use of the array
/object ArrayDemo2 {
  val array=new Array[int] (3)
  Val array2=new Array[int] (3)
  Val array3=new Array[int] (3)
  /**
   * Array Assignment operation 1
  /def init1 ()
  { Array
    (0) =1
    Array (1) =2
    Array (2) =3
  }
  /**
   * Array copy operation 2
  /def init2 ()
  {
    array2.update (0,4)
    array2.update (1,5)
    array2.update (2,6)
  }
  /**
   * OUTPUT data *
   *
  def out (Array:array[int])
  {
    val length=array.length
    for (i<-0 to Length-1)  //Note here I cycle
      println (array.apply (i))// Array.apply (i) ==array (i)
  }
  def main (args:array[string])
  {out
    (array3)
    init1 ()
    out (array)
    Init2 () out
    (array2)
  }
}
From the example code above, we can see that there are 2 ways to initialize an array:

One is:

    Array (0) =1
    Array (1) =2
    Array (2) =3

In Scala, things are objects, and arrays are no exception. Array assignment is actually done by a method.

The code above will be converted to two parameters by the compiler, which is the second method:

    Array2.update (0,4)
    array2.update (1,5)
    array2.update (2,6)

Notice how the above loops

The For loop here uses 0 to Length-1

, it is easy to understand that from 0 to length, the default step into 1.

When the method argument is only one, the parentheses and points are not written.

The complete formulation should be:

I<-0.to (length-1)


2. Cycle

Package Com.scala.demo/**  * mainly introduces the way of loops  */object ArrayDemo1 { //var a:array[int]={1;2;3;4;5;6;7;8;9}   /**    * use while    */  def ergodic1 (Array:array[int)) {    println ("Ergodic1
run! ")     var i = 0     while (I < array.length) {      println (Array (i))     & nbsp i++ or ==i can not be used in i+=1//scala, only use i+=1 or i=i+1    }  }  /**    * use the foreach   Java5 inside &N bsp;*/  def ergodic2 (Array:array[int]) {    println ("Ergodic2 run!")     Array.foreach (value =
> println (value))//is actually an anonymous method    /or this: Array.foreach (value:int) =>println (value)  }  /**    * uses a compact version of foreach ...    */  def ergodic3 (Array:array[int]) {    println ("E
RGODIC3 run! ")     Array.foreach (println)//Good Concise writing  }  /**    * use for loop try    */  def ERGODIC4 (Array:array[int]) {    println ("Ergodic4 run!")     for (Arg <-array)//for loop writing     {      println (ARG)    }  }   def main (args:array[string]) {    var array=new array[int] (3)     Array (0) =1     Array (1) =2     Array (2) =3     Ergod IC1 (array)     Ergodic2 (array)     ERGODIC3 (array)     ERGODIC4 (array)  }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.