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

    Swift数组是声明有以下几种,

    var countries:[String] = ["China","American","Russia"]

    var capitals:Array<String> = ["Bejing","Washington","Moscow"]

    var fruits = ["apple","orange","banana","pineapple"] //自动推断类型

    var defaultValueArray = [Int](count: 5, repeatedValue: 0) //建立一个默认值都为0长度为5的整形数组

    遍例数组有以下几种方式,第一种是像别的编程语言一样通过数组下标来循环遍例变量

    for i in 0..<countries.count{

        println(countries[i])

    }

     第二种是直接遍例

    for value in fruits{

        println(value)

    }

    第三种是借助一个Tuple来遍例数组,Tuple中第一个为下标索引,第二个为值

    for (index,value) in enumerate(fruits) {

        println("index:(index) value:(value)")

    }

    往数组中添加元素,此时数组中就有五个水果啦

    fruits.append("Lemon")

    在指定的位置添加一个元素

    fruits.insert("Waterlemon", atIndex: 0) 此时第一个元素就是Waterlemon啦

    说完添加就得说删除了,看以下的方法不用解释了,你懂得。

    fruits.removeAtIndex(0)

    fruits.removeLast()

    removeAll方法接受一个参数,允许清空数组时是否保持数组的容量,该参数默认值为false,即将数组的capacity容量设置为0。

    fruits.removeAll(keepCapacity: false)

    判断数组是否为空

    fruits.isEmpty

    反转数组

    fruits.reverse()

  • 相关阅读:
    < java.lang >-- StringBuilder字符串缓冲区
    Integer对象
    < java.lang >-- StringBuffer字符串缓冲区
    < java.lang >-- String字符串
    单例设计模式:★★★★★
    线程同步 Lock接口
    POJ 3254 Corn Fields (状压dp)
    Codeforces 583D. Once Again... (LIS变形)
    Light oj 1005
    Codeforces 543D. Road Improvement (树dp + 乘法逆元)
  • 原文地址:https://www.cnblogs.com/foxting/p/4399672.html
Copyright © 2011-2022 走看看