Swift # Array

Source: Internet
Author: User

//: Playground-noun:a Place where people can playImport Uikitvar str="Hello, playground."/*Arrays: Stores an ordered set of data array definitions: >OC: Array of values Nsarray *arr0 = @[@1, @2, @3]; Nsarray *arr1 = [Nsarray arraywithobjects:@1, @2, @3, nil]; NSLog (@ "%@", arr1), empty array Nsarray *arr2 = @[]; Nsarray *ARR3 = [Nsarray array]; NSLog (@ "%@", ARR3); immutable group: Nsarray variable array: nsmutablearray>swift:*///array with valuesvar arr0 = [1,2,3]var Arr1:array= [1,2,3]var Arr2:array<Int> = [1,2,3]var ARR3: [Int]= [1,2,3]//var arr4:int[] = [1, 2, 3] early notation//Empty Arrayvar ARR5 =[]var arr6=[Int] () var arr7= array<int>()//an array with an initial valuevar arr8 = Array (count:5, Repeatedvalue:1) println (ARR8)//immutable groups: var arr0 = []//variable array: let Arr0 = []/*element type: element type Oc:nsarray *arr = @[@1, @ "Lnj", @1.75]; NSLog (@ "%@", arr); Swift:*/var arr_02= [1,"LNJ",1.75]println (arr_02)/*if you want to make it clear that the array contains different types of data, you can use the Any keyword to indicate that the array can hold different types of data var arr:array<any> = [1, "Lnj", 1.75]println (arr)*//*array operations 1. Get length Oc:nsarray *arr = @[@1, @2, @3]; NSLog (@ "%tu", Arr.count); Swift:var arr = [1, 2, 3]println (Arr.count) 2. Determine if it is empty oc:nsarray *arr = @[]; NSLog (@ "%d", arr.count! = 0); Swift:*/var arr_01= [1,2,3]println (arr_01.isempty)/*3. Search Oc:nsarray *arr = @[@1, @2, @3]; NSLog (@ "%@", arr[0]); Swift:var arr = [1, 2, 3]println (Arr[0]) 4. Append Oc:nsmutablearray *arr = [Nsmutablearray arraywithobjects:@1, @2, @3, nil]; [Arr addobject:@4]; NSLog (@ "%@", arr); Swift:*/var arr02= [1,2,3]arr02.append (4);p rintln (Arr02) var arr03= [1,2,3]arr03+= [4]//Arr03 + = 4 Previous versions can be written like this//arr03 + = [5, 6, 7]Arr03 + = arr03[0...1]//and you can do it yourself.println (ARR03)/*5. Insert Oc:nsmutablearray *arr = [Nsmutablearray arraywithobjects:@1, @2, @3, nil]; [arr insertobject:@4 atindex:0]; NSLog (@ "%@", arr); Swift:*/var arr= [1,2,3]arr.insert (4, Atindex:0);p rintln (arr)/*6. Update Oc:nsmutablearray *arr = [Nsmutablearray arraywithobjects:@1, @2, @3, nil];arr[0] = @8; NSLog (@ "%@", arr); Swift:*/var Arr01= [1,2,3]arr01[0] =8println (Arr01) var arr0002= [1,2,3]arr0002[0.. <2] = [8,9]println (arr0002)/*7. Delete Oc:nsmutablearray *arr = [Nsmutablearray arraywithobjects:@1, @2, @3, nil]; [Arr removeobject:@1]; NSLog (@ "%@", arr); Nsmutablearray *arr = [Nsmutablearray arraywithobjects:@1, @2, @3, nil]; [Arr Removelastobject]; NSLog (@ "%@", arr); Nsmutablearray *arr = [Nsmutablearray arraywithobjects:@1, @2, @3, nil]; [Arr removeallobjects]; NSLog (@ "%@", arr); Swift:*/var arr001= [1,2,3]arr001.removeatindex (0) println (arr001) var arr002= [1,2,3]arr002.removelast () println (arr002) var arr003= [1,2,3]arr003.removeall (keepcapacity:false)//whether to maintain capacity if true, even if the deleted capacity still exists, the capacity is a multiple of 2println (arr003) println (arr003.capacity)/*Note: If an array is an immutable group, you cannot update/INSERT and delete the first version of the immutable variable group that can be modified*//*Rangeoc:nsmutablearray *arr = [Nsmutablearray arraywithobjects:@1, @2, @3, nil]; [Arr removeobjectsinrange:nsmakerange (0, 2)]; NSLog (@ "%@", arr); Swift:*/var arr_1= [1,2,3]arr_1.removerange (Range (Start:1, End:2) ) println (arr_1) var arr_2= [1,2,3]arr_2.removerange (0...0) println (arr_2)//Actually, range is the half-closed interval .var range =0...5//range =//the type can be inferred by an errorprintln (Range)//the type can also be inferred by printingvar range1:range<Int> =0...5//var range2:range<string>;//must comply with the Forwardindextype agreement//start start end Endvar range3:range<int> = Range (start:0, End:5) var range4:range<Int> =0.. <5println (range1) println (range3) println (range4)/*Oc:nsmutablearray *arr = [Nsmutablearray arraywithobjects:@1, @2, @3, nil]; Nsrange range = Nsmakerange (0, 2);//[Arr Replaceobjectsinrange:range withobjectsfromarray:@[@99, @88]]; [Arr Replaceobjectsinrange:range withobjectsfromarray:@[@99, @88, @77, @66]; NSLog (@ "%@", arr); Swift:*///Arr.replacerange (Range (start:0, End:2), with: [[+]Arr.replacerange (1.. <2, with: [ About, the, the, the]) println (arr)//equivalent to the previous line of codevar arr_3 = [1,2,3]arr_3[range (Start:0, End:2)] = [8,9]println (arr_3)//equivalent to the previous line of codevar arr_4 = [1,2,3]//arr[0...1] = [to ]//arr[0...1] = [in the [ ]println ("====--Traversal--====");/*4. Traverse oc://for loop nsarray *arr = @[@1, @2, @3];for (int i = 0; i < Arr.count; i++) {NSLog (@ "%@", Arr[i]);} For In loop for (NSNumber *number in arr) {NSLog (@ "%@", number);} Iterator Nsarray *arr0 = @[@1, @2, @ "1"]; [Arr0 enumerateobjectsusingblock:^ (id obj, Nsuinteger idx, BOOL *stop) {NSLog (@ "index =%tu element =%@", idx, obj);}]; Swift:*///For Loopvar arr_a = [1,2,3] forvar i =0; i < Arr_a.count; i++{println (arr_a[i])}//For in Loop forNumberincharr_a{println (number)} forIinch 0.. <arr_a.count{println (Arr_a[i])}//Remove the value of an interval range in an arrayvar arr_b = [1,2,3] forNumberincharr_b[0.. <3]{println (number)}//using enumerate iterationsvar arr_c = [1,2,3] for(Index, value)inchEnumerate (arr_c) {println ("index = \ (index) value = \ (value)")}

|--> Copyright (c) Bing Ma.

|--> GitHub RUL: https://github.com/SpongeBob-GitHub

Swift # Array

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.