Common methods for Swift arrays

Source: Internet
Author: User

var numbers = [0,1,2,3,4,5];
var vowels = ["A", "E", "I", "O", "U"];
var Emptyarr = [Int] ();
Number of array elements
Print ("The length of the array is: \ (numbers.count), \ (Vowels.count), \ (Emptyarr.count)");
Empty sentence
If Numbers.isempty {
Print ("The array is empty");
}
Use subscript to get elements
Print ("The second element of the array is: \ (vowels[1])");
Arrays cannot be crossed, for example: Vowels[-2]
The first element of an array
Print ("First element of array: \ (String (Describing:numbers.first))")
Minimum value of the array
Print ("Minimum value of the array: \ (String (Describing:numbers.min ()))");
Whether to include elements
If Vowels.contains ("A") {
Print ("array contains a");
}
Gets the subscript of an element based on the element value
If Let index = Vowels.index (of: "I") {
Print ("E's index value is \ (index+1)");
}
Iterating over array elements
For number in numbers{
Print (number)
}
Determine if an array is equal
Let ARR = [0,1,2,3,4,5];
If numbers = = ARR {
Print ("Two numbers equal");
}else{
Print ("Two array Not equal");
}
adding elements
Numbers.append (6);
Print ("added array \ (numbers)");
Print (Vowels + = ["F"]);
Array additions
vowels = vowels + ["G", "H"];
print (vowels);
Delete Element
Vowels.removelast ();
print (vowels);
Vowels.remove (At:2);
print (vowels);
Replace element
VOWELS[1] = "K";
print (vowels);

Common methods for Swift arrays

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.