scala-Basics-Arrays (1)

Source: Internet
Author: User

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)

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.