How to Create arrays in Swift3 and create arrays in swift3

Source: Internet
Author: User

How to Create arrays in Swift3 and create arrays in swift3

Reprinted from: http://blog.csdn.net/bwf_erg/article/details/70858865

An array is an ordered data set composed of a group of elements of the same type. The Set element in the array is

And can be repeated.

1. Create an array

In Swift, the array type format is:

Array <ElementType> or [ElementType]

ElementType in Array <ElementType> indicates the Array type, and <ElementType> indicates the generic writing method. [ElementType] is a short form. The two functions are the same. We prefer to use abbreviations. All array types in this book use abbreviations.

Next we will create an array of the String type.

1 | var strArray1: Array <String>

2 | let strArray2: [String]

When declaring an array, you can use let and var to modify it. let indicates an unchangeable array, and var indicates a mutable array.

The Code in line 1st declares a variable Array strArray1 of the Array <String> type. <String> it is a generic type, indicating that only data of the String type can be stored in this array.

The Code in line 2nd declares an immutable array strArray2 of the [String] type. [String] also declares an array that can only store String types.

Next we will learn how to create an empty array.

1 | var emptyStrs = [String] ()

2 | let emptyInts = [Int] ()

To create an array, declare and initialize the array. In the above 1st lines of code, we created a variable-type empty String array emptyStrs. The var Declaration indicates that the array is a variable array, and the String value in the brackets [] indicates the array type. [String] () is to initialize the array, but there is no element.

The Code in line 2nd and the Code in line 1st create an empty array emptyInts. The difference is that we declare an immutable array with let, which is of the Int type. The array declared by let is an unchangeable array and must be initialized at the same time. Once initialized, it cannot be modified.

Finally, we will learn how to create a non-empty array. The sample code is as follows:

1 | var strArray1: Array <String> = ["hello", "Swift"]

2 | var strArray2: [String] = ["hello", "swift"]

3 | let strArray3 = ["hello", "swift", 15]

The above Code declares and initializes the array. The array type is indicated by a colon. Elements in the array are enclosed by a pair of brackets ([]). Elements in the array are separated by commas.

The 1st line of code declares a String-type variable array strArray1 using a standard template and the initialization value is ["hello", "swift"]. The angle brackets <String> indicate the array type, indicating that strArray1 can only store elements of the String type.

The 2nd line of code explicitly declares the variable array strArray2 in short form. [String] indicates the array type, indicating that strArray2 can only store elements of the String type.

The Code in line 3rd declares an immutable array. The array type is not specified here, and implicit inference is used. The array type is inferred based on the value of the initialized array. In the strArray3 array, we store "hello", "swift" of the String type and 15 of the Int type. If the array type is not explicitly specified, we can store different types of elements in the array.

The immutable array has higher access efficiency than the variable array, and the variable array can be changed by sacrificing access efficiency. When we can determine that the array does not need to be modified, we should declare it as let. If the array content needs to be changed. We need to declare it as var.

In addition, if the array stores multiple identical elements, we can quickly create the array using the following methods:

1 | var threeDoubles = Array (repeating: 0.0, count: 3)

In this case, the content of the threeDoubles array is [0.0, 0.0, 0.0].

Related Article

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.