zoukankan      html  css  js  c++  java
  • Swift 数组

    // 数组的用法

    let empetyArray = []  // 空数组

     

    let stringArray = [String]()  // 字符串数组,限定其值为字符串

     

    let intArray = [Int]()       // 整型数组,限定其值为整型

     

    var shoppingList = ["苹果","香蕉","西瓜","木瓜","黄瓜","冬瓜"] // 声明一个四个字符串的数组

     

    print(shoppingList.capacity)

     

     

    // 增加

    shoppingList.append("芒果")     // 方法一

     

    shoppingList += ["荔枝"];       // 方法二

     

    shoppingList.insert("橘子", atIndex: 0) // 方法三

     

    print(shoppingList)

     

    // 常用用法

    print(shoppingList.count)    // 数组中元素的个数

    print(shoppingList.capacity) // 数组的容量最大为12

    print(shoppingList.isEmpty// 判断数组是否为空

     

     

    // 修改

    shoppingList[1] = "哈密瓜"

    print(shoppingList)

     

    shoppingList[2...5] = ["Apples","Banans"// 把下标为2345的值改为 "Apples","Banans"

    print(shoppingList)

     

    // 删除

    shoppingList.removeLast()

    print(shoppingList)

     

    shoppingList.removeAtIndex(3)

    print(shoppingList)

     

    //shoppingList.removeAll()

    //print(shoppingList)

     

     

    // 数组的遍历

    for item in shoppingList

    {

        print(item)

    }

    1
  • 相关阅读:
    Android apk 的安装过程
    Android布局技巧
    Android achartengine统计图
    Less适配移动端rem
    jquery.cookie.js时间设置
    前端面试题记录
    js身份证号码验证(小程序版)
    leetcode算法题笔记|Reverse Integer
    leetcode算法题笔记|two sum
    微信小程序多商品评价评星提交
  • 原文地址:https://www.cnblogs.com/fantasy3588/p/5068360.html
Copyright © 2011-2022 走看看