Scala learning document

Source: Internet
Author: User
Scala learning document directory

1. Array

2. List

3. Map

4. Break

5. If else

6.

7. foreach

8. Option

9. tuple

10. Code conventions

11. Request to accept array or array content

12.

Array usage

1) Scala Definition

General Definition: Val array1 = array [String] ("str1", "str2 ")

Array of specific length: Val array2 = new array [list [int] (Leng)

2) The array cannot be extended. Therefore, you can define arraybuffer during specific operations, and finally use toarray for conversion.

Val A: arraybuffer [String] = arraybuffer [String] ()

A + = "AA"

A. toarray

List usage

1) Scala Definition

Val list = list [String] ()

2) corresponding to buffer, similar to arraybuffer

Map usage

1) Scala Definition

Val map = map [int, string] (1-> "str1", 2-> "str2 ")

2) variable map operations

Val a = map [String, string] ()

A + = ("A"-> "B ")

Break usage

1) In Scala, the break and continue commands are removed, because more concise statements can be used in many scenarios.

// In Java ......

Int I = 0;

Boolean foundit = false;

While (I <args. Length ){

If (ARGs [I]. startswith ("-"))

{

I = I + 1;

Continue;

}

If (ARGs [I]. endswith (". Scala ")){

Foundit = true;

Break;

}

I = I + 1;

}

// In Scala ......

VaR I = 0

VaR foundit = false

While (I <args. Length &&! Foundit ){

If (! ARGs (I). startswith (""))

{

If (ARGs (I). endswith (". Scala "))

Foundit = true

}

I = I + 1

}

2) in the program, if you have to use the break and continue commands,

This plug-in is also packaged in the scala jar package.

Val mybreaks = new scala. util. Control. Breaks

Import mybreaks. {break, breakable}

Breakable {

Map. foreach {Case (K, v ){

If (k = 3 ){

Break

}

}}

}

If else usage

The return of scala results is the value of the last code executed, So If else has another function, just like a three-object operation.

Val res = If (true) "AA" else "BB"

In this way, the Res content is "AA", so we do not need to define res as VAR here.

For

For (ARG <-ARGs)

Println (ARG)

Use foreach

1) for the cycle of Map

Val map = map [int, string] (1-> "str1", 2-> "str2 ")

Val mapresult = map [String, string] ()

Map. foreach {Case (K, v) => {

Mapresult + = (k. tostring-> V)

}}

2) array Loops

Val array = array ['l', 'O', 'U', 'D', 'k', 'U', 'A', 'I', 'I', 'C ', 'M']

Array. foreach (A => {// here, A is a temporary variable and can be named at will.

// ...... Code

})

3) for the list Loop

Val list = new list [int] (1, 2)

List. foreach (AA => {

Println (aa)

})

Option usage

The focus of option is to determine whether the value to be determined is null, so that some objects can be retrieved directly before using the get method. Reduces the chance of exceptions.

1) generate the Option Method

// Retrieve records from the database. The corresponding records cannot be found by FID.

Val res = This. getsqlmapclienttemplate (). queryforobject ("dbconfigdao. Select", FID)

Res match {

Case null => NONE // if the database does not have a record corresponding to the FID, none is returned.

Case _ => some (RES) // If a record exists in the Database, RES is returned.

}

RES is the variable name defined above.

2) Use Option

// Resoption of the variable to be determined

VaR AA: String = NULL;

Resoption match {

Case some (dd) => AA = dd // dd is an arbitrary variable. We recommend that you use a meaningful variable name.

Case none =>

}

Tuple usage

1) generate tuple (tuples)

Define the tuple variable:

Val pair = (999, "Orange, I like ")

The function returns the tuple variable:

Def gettupletype (): (INT, string) = {

(999, "Orange, I like ")

}

Val pair2 = get

2) use tuple

Where I must be in tuple

Pair. _ I

Pair2. _ I

Note: currently, up to 22 tupe22 are supported.

Code conventions

Request to accept array or array content

1) When JS needs to input an array into the controller, it needs to encapsulate the content and encapsulate the content

Contenttype: 'application/json'

Data: JSON. stringify (uidarray)

The preceding settings are for parameters in Ajax.

Use

@ Requestbody UIDs: array [String]

In use, you can convert the string type in array to your own needs.

2) var is defined as Val on the service layer @ beanproperty.

VaR httpclientservice: httpclientservice = NULL

Process Service

Dao

Inherits the sqlmapclientdaosupport interface. Because Java can only be a single inheritance

1.

Ibatis

In ibatis

1. <sqlmap namespace = "dbconfigdao">

The namespace of this file is defined here. In other configuration files, you can use this name to call methods in this file, such as insert and update

2. <typealias alias = "dbconfig" type = "com. nineteenlou. Forum. dbconfig. Bean. dbconfig"/>

Alias: typealias, which is used only to define the bean type in the file. This can be easily used below. You can also directly use the class name.

3. <resultmap id = "dbconfigresult" class = "dbconfig">

The definition of the return type. The type of the resultmap is defined here in <select id = "selectlist" resultmap = "dbconfigresult"> below, you can set the data to be put back to the bean type.

4. <insert id = "insert" parameterclass = "dbconfig">

ID is used for calling. The input parameter type is the alias previously defined. If you want to return a value, You Need To resultclass = "string", that is, the returned value is of the string type.

5. <update id = "Update" parameterclass = "dbconfig">

If insert is used, you can select the return parameter.

6. <select id = "selectdstablepre" parameterclass = "map" resultclass = "string">

Select parameters can be of two types. Generally, map transmission is selected. You can use resultclass to select a required type. If you need to return bean, you can define it yourself.

Controllerhtmlaop from: https://docs.google.com/viewer? A = V & Q = cache: peboeonqboaj: Signature

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.