-
Creates and initializes a string Array
Val strArr1 = new Array[string] (3) strArr1 (0) = "how" strArr1 (1) = "is" strArr1 (2) = "you!"
The above code is transform as
Val strArr1 = new Array[string] (3) strarr1.update (0, "how") strarr1.update (1, "is") strarr1.update (2, "you!")
The Scala doc has a description:
/** Update the element at given index. * * Indices start at ' 0 '; ' xs.update (i, x) ' replaces The i^th^ elemen t in the array. * note the syntax ' xs (i) = x ' is a shorthand for ' xs.update (i, x) '. * * @param i the index * @param x the value to be written at index ' i ' * @throws ArrayIndexOutOfBoundsException if ' i < 0 ' or ' length <= i ' */ def update (i: int, x: t) { Throw new error () }
2. Create and initialize an array using the companion object (companion objects)
Val strArr2 = Array ("Fine", "thank", "you")
Here array is the name of an object, in Scala when the object appears in the position of the function, such as here, the object followed by parentheses, Scala's compiler will automatically look for this object's apply method, so here is actually called array.apply
In the Scala source, you can see
Array (E0, ..., en) is translated to {val a = new Array (3); a (i) = Ei; a} def Apply[t:classtag] (xs:t*): array[t] = {val array = new Array[t] (xs.length) var i = 0 for (x <-xs.iterator) {Array (i) = x; i + = 1} array}
The Apply method creates an object of class array for us and initializes it. The object array here is the accompanying object of the class array.
3. Similarly
Val Strarr = Array ("Fine", "thank", "You") println (Strarr (0))//Strarr (0) is essentially called strarr.apply
4. In array is a mutable type, list is immutable. Example of using list
Val twothree = List (2, 3) Val Onetwothree = 1:: Twothree//here:: A method of list, all operators (operator) in Scala are methods, and operators with the end of a colon are right Operator (parameter on left, object on right) println (Onetwothree)//List (1, 2, 3)
You can do that.
Val onetwothree = 1:: 2:: 3:: Nil println (Onetwothree)//List (1, 2, 3)
5. Why not add elements to the back of the list
Why isn't append to lists?
Class List does offer a "append" Operation-it ' s written: + and is
Explained in Chapter 24-but the operation is rarely used, because
The time it takes to append to a list grows linearly with the size of the
list, whereas prepending with:: Takes constant time. Your Options if
You want to build a list efficiently by appending elements are to prepend
them, then if you ' re-done call reverse; Or use a listbuffer, a
Mutable list that does offer a append operation, and when you ' re done
Call ToList.
The time complexity of appending elements increases linearly with the number of elements
You can append forward, then reverse
or first use Listbuffer and then ToList
Val L12345 = 1:: 2:: 3:: 4:: 5:: Nil val L54321 = l12345.reverse print (L54321)//List (5, 4, 3, 2, 1)
Import Scala.collection.mutable.ListBufferobject Testmain {def main (args:array[string]) {val lb = new Listbuffe R[int] Lb.append (1) lb.append (2) lb.append (3) var L = lb.tolist println (l)//List (1, 2, 3)}
6. Finally explain Val and Var
Both are variable modifiers in Scala, such as Static in Java, Final.
The function of Val is to declare a final variable, that is, a variable that cannot be assigned to another value.
var is the opposite of Val
In addition, there is no Java-like static keyword in Scala
7. Other ways to browse the list
List () or Nil
|
The Empty List |
| List ("Apple", "banana", "pare") |
Create a new list[string] |
| "Apple":: "Banana":: "Pear":: Nil |
Ditto |
COUNT (s = = S.length = 4)
|
Counts the number of string elements that has length 4 |
| Drop (2) |
Returns the list without its first 2 elements |
| Dropright (2) |
Returns the list without its rightmost 2 elements |
| exists (s = s== "until") |
Detemine whether a string element exists in the list |
| Filter (s = = S.length = 4) |
Return a list of all elements, in order, of the list that has Lengh 4 |
| ForAll (s = S.endwith ("L")) |
Indiates whether all elements in the list end with letter "L"
|
foreach (s => Print (s))
|
Executes the print statement on all of the string in the list |
| foreach (print) |
Same as Previous |
| Head |
The first element |
| Init |
A list of all and the last element |
| IsEmpty |
Indicates whether the list is empty |
| Last |
The last Element |
| Length |
Number of elements |
| Map (s = + s + "Y") |
Return a list resulting from adding a "Y" to each of the string element in the list |
| Mkstring (",") |
Makes a string with the elements of the list |
| Remove (s = s.length==4) |
Return a list containning al elements of the list except those that has length 4 |
| Reverse |
Returns a list in reverse order |
Sort
|
Return a list in alpabetical order |
| Tail |
Returns the list minus its first element |
Programming in Scala (Second Edition) Reading notes 3