Arrays in Scala

1. Introduction to array

There are two kinds of arrays in scala: fixed length array and variable length array.
  fixed length Array: because Array is immutable, initialization has a fixed length, so you can't directly delete its elements, or add more elements. You can only modify the element value at a certain location. To achieve deletion, you can generate a new Array by filtering. So there are no operations such as add, insert, and remove.
  variable length array: ArrayBuffer is variable and provides many element operations, including addition and deletion.
  conversion of fixed length array and variable length array: call toBuffer() and toArray() methods respectively.

2. Operations related to arrays

(1) declaration of array:

object Test01 {
    def main(args: Array[String]): Unit = {
        //1. Declare a fixed length array with new
        val arr1=new Array[Int](5)
        //2. Generate a fixed length array object by using the associated object
        val arr2=Array(1,2,3)
        //3. Declare a variable length array
        val arr3=ArrayBuffer[Int]()

        //4. Conversion between fixed length array and variable length array
        val arr4=arr1.toBuffer  //Fixed length -- > variable length
        val arr5=arr3.toArray          //Variable length -- > fixed length
    }
}

(2) addition, deletion and modification of array:

object Test01 {
    def main(args: Array[String]): Unit = {
        //Fixed length
        val arr1=Array(1,2,3)
        //Variable length
        val arr2=ArrayBuffer[Int]()
    }
    //Adding and modifying fixed length array
    def DMLARR(arr1:Array[Int]): Unit ={
        //increase
        arr1.+:(2) //Append the first part to generate a new array
        arr1.:+(2) //Append at the end to generate a new array

        //Change, the subscript of the array starts from 0, gets the subscript through arr1(index) and modifies it
        arr1(0)=5
    }
    //Addition, deletion and modification of variable length array
    def DML_Mul_Arr(arr:ArrayBuffer[Int]): Unit ={
        //increase
        arr+=2 //Tail addition
        arr+=(1,2,3) //Append multiple elements
        arr++=Array(1,2,3) //Append an Array
        arr++=ArrayBuffer(1,2,3) //Append an array buffer

        //insert
        arr.insert(0,-1,0) //Insert one or more elements in a location

        //delete
        arr.remove(0,2) //Starting from a location, remove several elements
    }
}

(3) array traversal:

object Test01 {
    def main(args: Array[String]): Unit = {
        val arr=Array(1,2,3,4)
        //Simple answer traversal of for
        for(item<-arr){
            println(item)
        }
        //Use subscript traversal
        for(i<- 0 to arr.length-1){
            print(arr(i)+"\t")
        }

        //Using until
        for(i<- 0 until arr.length){
            print(arr(i)+"\t")
        }
    }
}

(4) high order operation of array:

object Test01 {
    def main(args: Array[String]): Unit = {
        val arr=Array(1,2,3,4)
        //map method: change one element into another in the array
        arr.map(x=>x*5)

        //reduce method, which means to aggregate a bunch of elements into one element
        arr.reduce((x,y)=>x+y)

        //Filterm, filter the elements whose result is true and filter the elements whose result is false
        arr.filter(x=> if(x>2) true else false)

        //Count, according to some conditions, count the number of elements needed
        var filter_count=arr.count(x=> if(x>2) true else false)

        //Array summation
        var sun=arr.sum
        //Array maximum
        var max=arr.max
        //Minimum value of array
        var min=arr.min

        //Sort descending of array
        val ints = arr.sortWith((x,y)=>if(x>y) true else false)
    }
}

Tags: Programming Scala

Posted on Tue, 03 Dec 2019 23:20:57 -0500 by impfut