Scala Array detailed

Source: Internet
Author: User
Tags foreach array length arrays
1. Initializing an array

To initialize an array, you can use the New keyword, as in Java, to specify the data type and the array length.

    def test () = {
        val arr = new Array[int] (3)
        arr.foreach (x = print (x + ","))
    }

After the above code is run up:

0,0,0,

You can also provide the initial value directly without using the New keyword:

    def test () = {
        val arr = Array[int] (3)
        arr.foreach (x = print (x + ","))
    }

Note that when the run is up, the result is:

3,

From the above example, it is easy to see if there is a new keyword, the difference is still very large. 2. Initialize variable-length arrays

Many times, we need arrays of variable length. In Java, for example, ArrayList is often used. In Scala, there is the artifact of Arraybuffer. In order to see clearly the use of arraybuffer, directly on the source.

 def test () = {val Arrbuffer = Arraybuffer[int] ()//Add an element at the end of the array arrbuf
        Fer + = 1//Add multiple elements at the end of the array Arrbuffer + = (2,3,4)//Add another array at the end of the array Arrbuffer ++= Array (5,6,7) Printarr (Arrbuffer)//1,2,3,4,5,6,7,//delete last element Arrbuffer.trimend (1)//delete First element arrbuf Fer.trimstart (1) Printarr (arrbuffer)//2,3,4,5,6,//Insert element at specified position Arrbuffer.insert (1,0) print ARR (Arrbuffer)//2,0,3,4,5,6,//insert sequence at specified position Arrbuffer.insert (2,1,2,3) Printarr (arrbuffer)//2,0,1, 2,3,3,4,5,6,//delete the specified position element arrbuffer.remove (0) Printarr (arrbuffer)//0,1,2,3,3,4,5,6,//delete the specified A number of elements after the position arrbuffer.remove (0,4) Printarr (arrbuffer)//3,4,5,6,} def printarr (Arrbuffer:arrayb Uffer[int]) = {Arrbuffer.foreach (x = print (x + ",")) println ()} 
3.Array and Arraybuffer convert each other

If we need to convert between arrays and Arraybuffer, the operation is simple.

    def test () = {
        val arrayBuffer = ArrayBuffer (All-in-all)
        val res = Arraybuffer.toarray

        val array = array[int] (5,6,7 )
        val buf = Array.tobuffer
    }
4. Array Traversal

Collection traversal is the most common practice. General use for the loop can be done:

    def test () = {
        val array = array ("A", "B", "C", "D") for
        (i <-0 until Array.Length)
            println (i + ":" + AR Ray (i))
    }

After the code is run, the output is as follows:

0:a
1:b
2:c
3:d

If you want to adjust the step size to 2:

for (i <-0 until (array.length,2))

If you want to implement reverse traversal:

for (I <-(0 until (array.length)). Reverse)

There is an enhanced for loop in Java and this handy operation is naturally available in Scala:

For (item <-array)
5. Some practical operations

The array has some frequently used, particularly useful operations, also listed.

    def test () = {
        val array = array (1,2,3,4,5)
        println (array.sum)//15
        println (array.max)//5 println
        ( Array.min)//1
        println (array.mkstring ("-"))//1-2-3-4-5, often using println ("
        (", "-", ")" in actual projects)// (1-2-3-4-5)
    }
6. Some other operations

Using yield to produce a new array

    def test () = {
        val array = Array (2, 4, 5, 6, 7)
        val res = for (item <-array if Item% 2 = = 0) yield item * 2< C13/>res.foreach (x = print (x + ","))//4,8,12,
    }

The above operation is equivalent to the following code:

Array.filter (_% 2 = = 0). Map (_ * 2)

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.