3. Arrays and Lists

Source: Internet
Author: User

An array of arrays

1. Creating an array

Implicit: Val greetstrings = new Array[string] (3);

Explicit: val greetstrings:array[string] = new Arrray[string] (3);

2. Assigning values

(1) greetstrings (0) = "Hello";

Greetstrings (1) = ",";

Greetstrings (2) = "world\n"; Note that the parentheses () are behind, not []

(2) Val greetstrings = Array ("Hello", ",", "world\n");//The compiler infers that the type of the array is array[string], and that the back is () is not []

(3) greetstings.update (0, "Hello");//Change the element of 0 position

3. Traverse

for (i <-0 until Greetstrings.length)

println (i + ":" + greetingstring (i));

Ii. List of lists

1. Create a list

Val onetwothree = List (n/a);

Val Onetwothree:list[int] = List (n/a); Note that because the list is immutable and cannot be assigned to an array, the instance is assigned a value at the time of the instance.

2. Traverse

(1) for (i <-0 until Onetwothree.length)

println (i + ":" + onetwothree (i));//same array, random access is List (i), not list[i]

(2) println (Onetwothree); Note that arrays cannot be printed so directly

(3) Onetwothree.foreach (print)

3. List stitching

(1) List has a method called ":::" for two list stitching.

Val one = List (UP);

Val-n = List (3,4);

var oneaddtwo = one:::;

(2) Add an element "::" in front of the existing list

Val three = 5:: one;

Val one = 1:: 2:: Nil; Create one = List (UP); Nil represents an empty list.

A classmate to ask, why not add elements at the end of the list, not to add to the table header? In fact, the list.append (or +) method provides the ability to add elements at the end of the list footer.

However, the operation time of adding elements at the end increases linearly with the size of the list, and uses:: Only constant time is required to add to the table header.

If you do not want to add elements at the end of the table, we can achieve efficient tables by adding the table header in turn, and then calling the List.reverse method to deserialize the table.

(3) Some common methods

Val thrill = "would":: "Fill":: "until":: Nil;

Thrill.count (s =>s.length = = 4); Counts a string of length 4 in thrill and returns 2

Thrill.drop (2);//Returns a list of the first two elements removed

Thrill.dropright (2);//Returns a list of the two elements that are removed from the tail

Thrill.exist (s = = s = = "until");//Determine if thrill contains "until"

Thrill.filter (s = s.length ==4); Returns an element of length 4 in thrill

Thrill.filternot (s = s.length ==4); Returns an element with a length not 4 in thrill

Thrill.forall (s = = S.endswith ("L"));//Determines whether thrill all elements end with L and returns a Boolean

Thrill.foreach (print);//Traverse

thrill.head;//returns the first element of thrill

thrill.last;//returns the last element

thrill.init;//returns a list of elements other than the last element

thrill.tail;//Returns a list that consists of elements other than the first element

Whether the thrill.isempty;//is empty

thrill.length;//length

Thrill.map (s = + s+ "y");//Add y to each element of the list

Thrill.mkstring (",");//Returns a string consisting of all elements of the list, "Will,fill,until"

Thrill.reverse;//List reversal

Thrill Sort ((s,t) = S.charat (0) toLower > T.charat (0) toLower);//Sort the first character

Three, array and list of relations and differences

A scala array is a mutable sequence of objects of the same type, meaning that an array can contain only elements of the same type. Although it is not possible to change the length of an array after it is instantiated, it can change its element value, so the array is a mutable object

Scala lists, like arrays, can only contain elements of the same type, but the list is immutable, and each change to the list will return a new list, which is a bit like a string in Java.

Note that the list in Scala and the list in Java have the same name, but not a single thing, which is quite different.

3. Arrays and Lists

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.