When I started learning Scala, I felt like learning Java.
First, download the scala package from the website and install it. After installation, put the bin of the installation directory in the environment variable psomething.
Enter Scala in cmd. If Scala appears, congratulations. The installation is successful ~~
1: Variable
First, let's look at the variable definition method.
VaR can define variables and change values.
The values defined by Val cannot be changed, similar to the final variable of Java;
2: Functions
Defines a function
Def max (X: int, Y: INT): Int = {
If (x> Y) x
Else y
}
Def: Surface definition function
MAX: function name
The two parameters of the (X: int, Y: INT) function are int type. Note that int cannot be lowercase;
: INT: The return type is int.
Function subject in braces ~
Can be called directly
Max (9, 10)
If there is no parameter and the function does not need to be returned, you can write it as follows:
Def greet () = println ("hello ")
Exit Editor: Quit
3: Scala script
You can write Scala scripts in a text file.
As follows: args. foreach (ARG => Print (ARG ))
Name: test. Scala
Then, run the scals test. Scala Ni hao me command in cmd.
Ni Hao me is passed into the script as a parameter and printed;
Script parsing:
The foreach method is called in the script, and Arg is passed as the parameter => followed by the function body for printing;
For Loop:
Scala's for loop is as follows:
For (ARG <-ARGs) println (ARG)
4: array (araay)
Scala Arrays can be as follows:
VaR gree = array ("Ni", "hao", "")
For (I <-0 to 2) println (gree (I ))
This completes loop printing of an array;
5: List)
Val onetwolist = List (1, 2, 3) // This is a new list
List Method:
: Indicates that the two lists can be combined.
: Yes. You can add the element to the front of the list.
However, the list does not have a method to add an element. An empty list is called nil. However, you can combine empty lists and elements to form a new list.
Summary of the list method:
List () or nil --- empty list
Val thrill = "I": "am": "niuxiao": Nil ---- create a list
List ("1", "2"): List ("3", "4") ---- overlays two lists
Thrill (2) ---- returns the element whose subscript is 2.
Thrill. Count (S => S. Length = 4) --- returns the number of string elements whose length is 4 in the thrill list.
Thrill. Drop (2) --- Delete
Thrill. dropright (2) --- delete from the right
Thrill. exists (S => S = "am") --- returns whether an AM element exists.
Thrill. Filter (S => S. Length = 4) --- return a new list composed of 4 elements.
Thrill. forall (S => S. endswith ("1") --- returns whether all elements in the list end with 1.
Thrill. foreach (print) --- print elements in a loop
Thrill. Head --- return the first element in the list
Thrill. init --- returns a list of other elements except the last one.
Thrill. isempty --- return whether the thrill list is empty
Thrill. Last
Thrill. Length
Thrill. Map (S => S + "4") --- return to the list consisting of "4" for each string element in the list.
Thrill. mkstring (",") --- returns a string consisting of elements in the list, connected
Thrill. Remove (S => S. Length = 4)
Thrill. Reverse --- return a new list of list elements in reverse order
Thrill. sort (S, T) => S. charat (0 ). tolowercase <t. charat (0 ). tolowercase) ----- returns a new list composed of the first letter, lowercase letter, and sorted by the list element.
Thrill. Tail --- return a new list composed of the first element
6: tuple)
Differences between tuples and lists:
Lists are all elements of the same type. tuples can be elements of different types.
Example: Val pair = (99, "nihao ")
Println (pair. _ 1)
Println (pair. _ 2)
Very convenient ~~