ImportJunit.framework.TestCaseImportScala.collection.mutable.ArrayBuffer;//Arrays (1)//knowledge points-Define arrays, variable-length arraysclassDemo1extendstestcase{//Defining Arraysdef test_^^(){ //define array Syntax 1 val variable name = new array[class type] (length) equivalent to int indexs [] = new int [n] in Java;Val nums =NewArray[int] (10) //defines the array Syntax 2 val variable name = Array (the specific value, separated by commas) equivalent to the int indexs [] = {n/a} in Java; //the array type is inferredVal cums = Array () //accessing elements in an array by array variables (subscripts) instead of array variables [subscript]Val N1 = nums (1)//equivalent to int n1 = nums[1]; } //variable-length arraysdef [email protected]@ () {//define variable-length array Syntax 1 val variable name = new arraybuffer[class type] ()Val nums =NewArraybuffer[int] ()//add elements to the tail endNums + = 10//add multiple elements to the tail endNums + = (5,5,9,10) //add additional arrays or collections to the tail endVal cums = Array (1,2,3,4,5) Nums++=cums//inserts an element in the specified subscriptNums.insert (1, 0) Nums.insert (1, -1,-2,-3) //Remove 2 elements from the tailNums.trimend (2) //remove ElementNums.remove (2) //Remove the specified subscript several elementsNums.remove (1, 2) //variable-length arrays to fixed-length arraysNums.toarray//array to variable-length arrayVal kums = Array (0); Kums.tobuffer}//iterating through an arraydef test_&&() {Val cums= Array (All-in-) //Positive sequence Traversal for(I <-0until Cums.length) println (cums (i) ) for(I <-0. Until (cums.length)) println (Cums (i) )//0 until 10 returns 0 to 9//0 to 10 returns 0 to ten//actually 0 until 10 bottom is a used method call: 0.until (Ten)Val k = 0 until 10//The reverse traversal of the Access function ignores the () and ignores the premise of a single parameter or no parameter . for(I <-(0. Until (cums.length)). Reverse) println (cums (i) ) for(I <-0until cums.length reverse) println (cums (i))//Traversal interval for(I <-0until Cums.length) println ()//Enhanced for Loop for(Ele <-cums) println (ele)}}
scala-Basics-Arrays (1)