Scala learning-array/ ing/tuples

Source: Internet
Author: User

Array/ ing/tuples

 

I. Array

1. Fixed Length Array

Two forms of declaring an array:

  • Declare the Val array name of the specified length = new array [type] (array length)

  

  • Provides an array of initial values without the new keyword.

  

Scala declares an array with an array class name and uses () to specify the length or provide an initial value sequence.

In JVM, scala array is implemented in Java array mode. For example, the type of ARR in JVM corresponds to Java. Lang. String [], and chararr corresponds to Char [].

2. variable-length Array

  

Arraybuffer, short for scala. collection. mutable. arraybuffer, is similar to the arraylist in Java and the vector in C ++. It is a variable-length array structure called Array Buffer.

Declared by Val name = arraybuffer [type] (). You must specify the type when declaring arraybuffer.

Add elements or sequences using the plus sign (+ =) operator, and concatenate Arrays Using the plus sign (+ =) operator.

[Adding or deleting elements at the end of arraybuffer is an efficient operation .] Arraybuffer also supports inserting and deleting elements at specified locations.

Arraybuffer to array Conversion: Toarray Method

  

3. array Traversal

Traverse arrays through a For Loop

  

Specify the step size for traversing the array-through until (upper bound, step size)

  

  

Use the reverse function to traverse the array in reverse order:

  

4. You can use the for loop + yield to obtain a new array or arraybuffer.

  

  

After you use for + yield to operate an array or arraybuffer, a new array or arraybuffer is obtained.

5. Common functions for operating arrays/array Buffering

  

Sums, calculates the maximum and minimum values, and sorts arrays. When the sorted function is used to sort arrays or arraybuffer, a new array or arraybuffer is returned, and the original one remains unchanged.

You can directly call the Sorting Algorithm for arrays.But cannot sort the arraybuffer.

  

Quicksort directly modifies the original array, while sorted returns the new array..

6. Multi-dimensional array

The multi-dimensional array in Scala is the same as that in Java. Multi-dimensional arrays are arrays.

  • Use array. ofdim [type] (Dimension 1, dimension 2, dimension 3,...) to declare a multi-dimensional array, such as a two-dimensional array;

  

From the initialization of two-dimensional arrays, we can see that multi-dimensional arrays are also arrays in Scala.

  • Array [array [int] (Dimension 1) can be used to declare an irregular array;

  

A multi-dimensional array is an array. To declare a multi-dimensional array based on this attribute, for example, a two-dimensional array in the example, you must specify the size of the most peripheral array when declaring the array.

[Note: You can use the implicit conversion method in the scala. collection. javaconversions package to convert the scala container class to the class in Java .]

 

Ii. ing

1. ing Construction

Dual, That is, name-value pair. You can use the-> operator to define the dual,Name> ValueThe result is (name, value );

  

Yes.Declare dual Variables:

  

IngA ing is a set of duals.

  • StatementImmutableIng. When map is directly used for declaration, the default value is immutable ing type.

  

[Note: immutable ing maintains the element insertion sequence .]

  • StatementVariableScala ing (scala. collection. mutable. map)

  

[Note: values placed in the variable ingNoSort by order .]

You can use the for loop to modify values in a variable map;

  

[Note: The variable and immutable ing indicate whether the entire ing is variable,Including element values, number of elements in the ing, and element order.]

  • Declare empty ing

An error is reported when new ing is defined directly through new map [Type 1, type 2] () Because map is abstract and cannot be instantiated.

When defining an empty ing, You need to specify the ing implementation class, which is defined by new;

  

Defines immutable ing and mutable ing respectively. Note: An error is reported when you use the new hashmap definition directly.

2. Common ing operations

  • Determines whether the ing contains a key: map. Contains (key value)

  

  • Use + =VariableIngAdd element or join ing:

  

  • Use-= removeVariableKey and corresponding element in ing

  

  • You can use the + operator to return a new immutable ing. You can use the-operator to return a new immutable ing;

  

  • Map Traversal

  

  • Obtains the keyset and value set of the ing key set.

  

By ingKeysetThe ing method can be used to obtain the set composed of key values. The values method can be used to obtain the interable object of the ing value set, which is applied to the loop.

Scala's keyset/values method is similar to the ing method in Java.

3. The underlying implementation of scala ing in Scala is either based on a hash table or a balance tree, where the ing Based on the Balance Tree is ordered internally.

  

Scala currently only provides an unchangeable version of Balance Tree-based ing.

[Note: You can use Java treemap to build a variable-order ing .]

4. In variable ing, you can use linkedhashmap to build a ing that maintains the element insertion sequence.

5. Scala. collection. javaconversions. mapasscalamap can convert Java map to Scala map. collection. javaconversions. mapasjavamap converts Scala ing to Java ing.

6. tomap Method

The tomap method can be used to convert a set of dual components into a ing.

  

 

3. tuples

1. tuples are the aggregation of different types of values;DualIs the simplest tuples.

2. tuples

Enclose different values in parentheses to indicate tuples.

  

In the preceding example, the group type is (INT, double, Char, string). The values of different types can be stored in the tuples.

3. tuples

  • Elements in tuples are called group elements. You can use _ 1, _ 2, and _ 3 to access the corresponding sub-object group metadata.

  

[Note: The subscript of the group element in the tuples starts from 1 .]

  • Access values in tuples through pattern matching

  

  Ignore unwanted values. During pattern matching, _ (Universal match character) is used to hold group elements that do not need to be retrieved. Each _ can only hold a group of metadata.

  

4. tuples can be used when a function returns multiple values.

  

In the above example, the type of the Function Definition return value is tuples (INT, string );

  

 

Scala learning-array/ ing/tuples

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.